Style Guide for CS 159

The most important role of a set of coding standards is consistency. No group of programmers will ever completely agree on the best way to format code. Should code blocks be indented with tabs? With spaces? How many spaces? Two? Four? Eight? Where should opening braces be placed? The fact is, none of these things matter very much. What does matter is consistency. It is very inconvenient for a group of people to work together on a shared code base if they don’t agree on some set of common standards.

Rather than enumerating a long list of formatting requirements for this course we will use automated tools, both to confirm that formatting standards are satisfied and to automate the process of formatting our code. If you haven’t already, follow these instructions on Installing Java and Eclipse. This will walk you through the process of configuring Eclipse to use Checkstyle and the Eclipse Auto Formatter to satisfy the code formatting requirements for this course.

These tools are only useful for low-level formatting issues like indentation and line length. It is possible to write very bad code that is properly formatted and will pass automated checks. See below for some additional style requirements and suggestions.

Names

Selecting informative names is one of the most important steps in writing readable, maintainable and correct code. Variable names should clearly describe what the variable is intended to contain.

All names should be descriptive and readable: subTotal rather than s; grade rather than grd. Multiple-word names should use a capital letter to separate words (e.g. subTotal). Variable names should balance clarity with brevity. The name person is better than currentPersonObject. However, per is worse than either (percentage? person? permitted?).

Comments

The point of comments is to clarify code that might otherwise be difficult for the reader to follow. Well organized Java code with informative variable names should require few comments inside of method bodies.

Documentation

Every class must include a Javadoc comment like the following.

All methods (except simple getters and setters) must contain a Javadoc comment preceding the method header as shown in the example below. The description should describe the “black box” behavior of the method (what it does).

Acknowledgments

Every programming assignment must contain the following section which follows the class description or must cite any sources used (such as a TA). You should either include this statement in all files or minimally it may appear in the file containing your main method.

OR

This acknowledgment is not necessary for lab assignments. Note the single star after the slash: this is not a Javadoc comment.

Miscellaneous