Coding Guidelines v2 - Summary
Coding Guidelines v2 - Summary
Motivation. Using good programming style, common idioms, and best practices is essential to produce reliable
software efficiently. Good software is quicker to build, cheaper to maintain, and more fun (or at least less difficult) to
work with. But no set of rules can cover all situations - you must also have guiding principles. A good start would be
to use the following.
Readability - Make your program as readable as possible ( good names, spacing, ...).
Simplicity - Don't add unnecessary complication and duplication. KISS.
Convention - Use standard conventions and good practices as much as possible.
Indentation
Use either K&R or Allman indentation style. Don't use others.
Indent size 4 spaces
Blanks, not tabs
Spaces and blank lines - Use them to make the source more readable
No space between method name and the left parenthesis. f(x), not f (x).
One space between keywords (if, while, for, switch, ...) and the left parenthesis.
Spaces around assignment and many other operators.
Add // comment if meaning of variable isn't completely clear or if has special range, values, etc.
Don't reuse variable name for more than one purpose. Variables are cheap.
Give variable the least required scope (without adding extra blocks).
Declare within for loop header if possible.
Don't hesitate to create extra local variable if it improved readability.
Instance variables (fields)
3. Braces
Use them, even for single statements.
Use plural names of mass nouns for arrays and data structures.
Avoid confusing name duplicates (method same as field, differing only by case, ...).
Throwing exceptions
Don't throw Exception - make it more specific.
Methods
Don't assign to parameters; it makes code more difficult to read.
Consider declaring parameters final to assure the reader they aren't changed.
Use multiple returns only if it makes the code clearer, otherwise return only at the end.
Methods should fit on one page/screen. If they're bigger, consider splitting them.
Avoid recursion unless it's a situation where it's much better (eg, traversing trees).
Loops
Use for instead of while if it groups everything in one statement.
For loop clauses should be coherent -- all related.
Use the enhanced for (foreach) when possible.
Don't change a for loop iteration variable in the body of the loop.
Use break to exit early or to terminate an "infinite" loop.
If
Do not write an empty true or else clause.
Writing positive logical expressions is more readable then negative. Change if easy.
Data structures
Use the generic forms of Java Collections data structures.
Use newer rather than older versions: HashMap<...> instead of HashTable, ArrayList<...>
rather than Vector.
Methods
A method should be small and focused on one task.
Split a method into several methods if it operates on different levels of data.
Split a method if it becomes too large (longer than one screen is a common guideline).
Split a method which does different things.
Smaller methods are easier to understand, use, and debug.
It's OK to have utility methods that are only called from one place.
Not covered here. There's lots not covered in this list: Object-Oriented Design and Programming guidelines,
including Patterns, GUI design guidelines, and development methodologies.
References
Code Complete 2nd Edition by Steve McConnell, Microsoft Press 2004, may be the single best book that addresses
coding guidelines. Several languages are included in the examples, but the thoughtful commentary is very useful. An
excellent book for the intermediate programmer.
Sun's guidelines. Perhaps the most common coding guidelines are Sun's Code Conventions for the Java
Programming Language, which can be read online of downloaded in several forms from
java.sun.com/docs/codeconv/. The guideline in the list above have a lot in common with the Sun Guidelines,
although they differ on a couple of issues (eg, naming instance variables).