Java Style

Chapter: Java Style

Oracle can provide you with a comprehensive idea of code conventions for Java and explain why it is important to follow the code. You can find a .pdf or postscript file that you can download or you can just view the .html version here.


Exercise 2

Read the Java code conventions. Then find an old Java program that you wrote yourself. (If you are new to Java then it is ok for you to find somebody else's Java program through a web search.) Load that file into your favorite editor (presumably Emacs, but feel free to disagree) and put in comments indicating where the author has diverged from the coding conventions. Still within the comment, write equivalent code that does meet the conventions. Express your opinion as to whether readability and comprehensibility are improved by adhering to the conventions.

Perhaps it's a little tedious to go through your own code looking for stylistic violations. It will please you to know that there is a plugin for Eclipse that will do the job for you. You can download the checkstyle plugin from here.

Be sure to follow the appropriate instructions for installing the plugin (it will vary with which version of eclipse you have installed).


Exercise 3

Copy the Java program you used for the previous exercise into a Java project in your eclipse. Use the checkstyle plugin to see how well your code did. Take a screenshot clearly showing all the complaint places that are shown. Incorporate that screenshot into your lab notebook. Add your own comments about how picky you think the checkstyle plugin is. Do you like having your code scrutinized to this degree?

When and if you are ever coding for a living, your employer may well have very exacting standards for code style and documentation. For example, you can be fairly certain that Sun and now Oracle expect the code for the Java API to be pretty good.


Exercise 4

Copy the source code of some of the classes you often use into your Java project. Use Eclipse and its plugin to check its style. (You'll need to replace the source code package by one that suits your project. Even then there may be some errors because visibility isn't quite what it should be. Nevertheless you should be able to get a very clear idea of how well the Sun/Oracle engineers adhered to the coding conventions.) Write your reactions as to how well the authors did as far as coding conventions and style goes.


rhyspj@gwu.edu