Coding at the Lowest Level

Coding Patterns for Java Beginners

Joseph Bergin

The paper provides advice to Java beginners. The purpose is to make the intention of the programmer known, and to make the code understandable for maintainability. This is a "how-to" paper for designing Java programs.

The patterns can be grouped into seven categories. The categories and patterns are:


  Rewrite – Plan on rewriting the program several times & read it between rewritings.

  Piecemeal Growth - When writing a large program, consider what small program inside it you can get working first.


  Brace All – Completely brace all statement parts in all structures.

  Braces Line Up – When writing braces (or parentheses), if the opening and closing symbol don’t fit on the same line, then make

    them align vertically.

  One Liner – "If the entire structure fits comfortably on one line, then put it on one line."

  Indent for structure – The parts of a structure should be indented from the keywords and punctuation symbols, but don’t indent

    too much.

  Consistent Capitalization – "Capitalize consistently to show the kind of thing a name refers to." (See Standards)

  Public Features First – List the public parts of a class first, and the private parts last.


  Say It Once – Say each thing in your total program once, or it may lead to inconsistencies.

  Polymorphism Before Selection – Organize the code so that decisions are made via the polymorphic nature of the system, not with

    if and switch statements.

  Short Method – Methods should be short (10 lines is very long).

  One Service Per Class – Give each class one job to do.


  Locals (Only) When Needed – "Use local variables when they are used to avoid duplicate computation and otherwise avoid their


  Assign Variables Once – Once a variable has a value, do not change it. Preferably assign the value when the variable is declared.

    (Immutable Objects are a good thing!)

  Local Variables Reassigned Above Their Uses – If you must change the value of a variable, arrange the code so that the change

    occurs above any subsequent use.

  Function For Complex Condition – Any complex condition should be a separate method, expressing a positive condition. Then

     call the method in the if or while statement instead of writing out the complex condition.

  Complete Interface – Provide a set of public methods in a class that permits client software to perform all legal transformations.


  Comment (Only) When Necessary – Make comments indicate the intent of the code.

  Intention Revealing Name – All names in a program should denote the purpose of the item they refer to.

  Spell It Out – Don’t abbreviate!

  Consistent Naming – Use a consistent style. (See Standards)

  Name the Constants – Name any important constant values, and thereafter refer to them only by name.

  Optimize For Readability – Make the code as clear and correct as possible, not necessarily as fast as possible.

  Logical Interface – The interface of a class should define its logical services, not its implementation.

  Say "this" – When referring to a field of the current object, prefix the reference with "this".


  Composed Method – Break up long or complicated methods.

  Method Object – Defines how to break up a long method (See paper)


  Strong Encapsulation – Don’t reveal the details of the implementation.

  Private Fields – All fields should be private.

  Initialize – When a field or local variable is declared, initialize it immediately.