Home Monash Info News & Events Campuses and Faculties Monash University
The School of Network Computing
The School of Information Technology Degrees Subjects Research About Us Our Staff Links Campus
The School of Information Technology Contacts International Students Pages for Students Pages For Staff

Style Guide

  1. Naming conventions

    1. Package names

    2. Class names

    3. Variable names

    4. Constant names

    5. Method names

    6. Abreviations and acronyms

    7. Generic variable names

  2. Code layout and white space

    1. Line lengths

    2. No tabs, no page breaks

    3. Indentation

    4. Block layout

    5. White space

    6. White space in method names/calls

    7. Blank lines between methods

    8. Declarations

  3. Commenting

    1. General comment policy

    2. Non Javadoc comments

    3. Javadoc comments

  4. Further resources

  5. References

 

Naming conventions

Package names

Package names should all be in lower case:

   mypackage, java.beans, java.beans.beancontext 

Package naming convention used by Sun for the Java core packages. The initial package name representing the domain name must be in lower case.

Class names

Class or type names hould be nouns and written in mixed case starting with upper case:

   MyClass, Object, ArrayList

Naming convention used by Sun in the Java core packages

Variable names

Variable names should be in mixed case, beginning with lower case:

   myVariable, newName, nextItem

Naming convention for variables used by Sun in the Java core packages. Makes variables easy to distinguish from classes, and easily allows a variable to be given the same name as its type if appropriate: ArrayList arrayList.

Constant names

Names for constants (any variable declared as final) must be all uppercase using underscore to separate words.

   MAX_HEIGHT, EXIT_ON_CLOSE, TOP_ALIGNMENT

Naming convention used by Sun for the Java core packages. Alternative: in many cases implementing the value as a method is a better choice:

   int getMaxIterations() // NOT: MAX_ITERATIONS = 25 
   {
      return 25;
   } 

This form is easier to read, and ensures a uniform interface towards class values.

Method names

Method names should be verbs and written in mixed case starting with lower case.

   myMethod, getWidth, setName, start

Naming convention used by Sun for the Java core packages.

Abreviations and acronyms

Abbreviations and acronyms should not be uppercase when used as name.

   openHtmlPage();      // NOT openHTMLPage
   exportUmlToDisk();   // NOT exportUMLToDisk

Having an acronym all in uppercase does not make the acronym stand out in a name and reduces readability.

Generic variable names

Generic variable names should have the same name as their type.

   void setDimension (Dimension dimension) 
   // NOT: void setDimension (Dimension value)
   // NOT: void setDimension (Dimension aDimension) 
   // NOT: void setDimension (Dimension x) 
   void connect (Database database) 
   // NOT: void connect (Database db)
   // NOT: void connect (Database oracleDB) 

This reduces the complexity of your code and makes it easy to deduce the type of a variable from its name. If for some reason this convention doesn't seem to fit it is a strong indication that the type name is badly chosen.

Generic or non-generic? Non-generic variables have a role to perform in your code. These variables can often be named by combining role and type:

   Point startingPoint, centerPoint;
   Name loginName;

Code layout and white space

Line lengths

Keep your lines to 80 columns maximum. 80 columns is the common dimension for editors, terminal emulators, printers and debuggers, and files that are shared between several developers should keep within these constraints. It improves readability when unintentional line breaks are avoided when passing a file between programmers.

No tabs, no page breaks

NEVER use tabs or page breaks in your code. These characters are interpreted differently by different programs in different environments and will cause your code to look differently when other programmers view it (i.e. your marker!).

Always make sure your editor (Bluej included) is using tabs only - and not spaces.

Split lines

   newTotal = a + b + c +
      d + e);

   myMethod (int firstNumber, int secondParam,
      int thirdParam);

   setText ("Long line split"
      "into two parts.");

   for (index = 0; index < maxValue; 
      index += indexStep) 

Split lines occurs when a statement exceed the 80 column limit given above. It is difficult to give rigid rules for how lines should be split, but the examples above should give a general hint.

In general:

Indentation

Indentation standard is three spaces. Make sure your indentation is kept regular throughout your code - each new block should have another level of indentation.

Warning: if you have more than three levels of indentation inside a method, consider making a new method out of that code.

Block layout

Use one of the first two; never use the third:

   // OK to use

   for (int index = 0; index < maxValue; index ++) {
      doSomethingInteresting ();
      someValue = someThingElseInteresting ();
   }

   // OK to use
   for (int index = 0; index < maxValue; index ++) 
   {
      doSomethingInteresting ();
      someValue = someThingElseInteresting ();
   }

   // NEVER use!
   for (int index = 0; index < maxValue; index ++)
     {
       doSomethingInteresting ();
       someValue = someThingElseInteresting ();
     }

Use the same rules for try-catch-finally, if, if-else, while, do, for, case, synchronized, method and class blocks.

White space

Use the following rules for putting space in your code lines.

Examples:

   a = (b + c) * d;           // NOT: a=(b+c)*d
   while (true) {             // NOT: while(true) ...
   doSomething (a, b, c, d);  // NOT: doSomething (a,b,c,d);
   case 100 :                 // NOT: case 100:
   for (i = 0; i < 10; i++) { // NOT: for (i=0;i<10;i++){ 

This helps to make parts of your code stand out from each other, improving the readability of your code.

White space in method names/calls

Method names should be followed by a space when there paramaters.

   doSomething (paramaterValue); // NOT: doSomething(paramaterValue); 
   doSomethingElse();            // NOT: doSomethingElse ();

This helps to make the method name stand out from the paramater names. When there are no paramaters, leave out the space because there can be no doubt about the name of the method.

Blank lines between methods

Always use three to five blank lines between methods - but decide on the number and keep it consistent!

Declarations

Only declare one variable per line.

   int one, two, three;          // NO: not very readable
   int one;                      // YES: much clearer
   int two;
   int three;

 

Commenting

General comment policy

Comment like your life depends on it!

Non Javadoc comments

Single or double line comments, use the following form:

   // This type of comment can be one 
   // or maybe two lines

Non javadoc comments that take up more than two lines should be in a multi-line comment box. Note the indentation used!

   /*
      This is a more substantial comment
      that the author feels important enough 
      to put here over several lines
   */

Javadoc comments

javadoc faq section

Further resources

 

References:

[1] Java Programming Style Guidelines, Version 3.0, January 2002 for Geotechnical Software Services

http://geosoft.no/javastyle.html

 

 

 

 

HelpSite MapContactsIndexQuick Search

 

These pages were created by Robert Mark Bram using Macromedia DreamWeaver UltraDev 4.0.

Have something to ad? Comments or corrections? Tell me about it now!