30 January 2007

Java Coding Standards


Here I am writing some of the best practices & standards of java coding. These are some of the key points to be noted while writing a good java code.

STD-1 Package naming.
Package names contain only lower case letters. Never create a package which
uses a package prefix already being used by another entity, for example “javax”.

STD-2 Class and Interface naming.
Class names should be nouns using mixed
case with embedded words capitalized. Do not use embedded underscores within names.
Avoid acronyms and abbreviations unless they are already more widely used than their long
form (e.g. HTML). When using acronyms do not capitalize the whole acronym. Instead
treat it as a word and only capitalize the first letter.
Interface naming follows class naming exactly.
Proper class name examples:
MyDriver
MyClass
HtmlConverter

STD-3 Method naming and formatting.
Methods are active and, therefore, should be named using verbs. As with class names, use
mixed case except the initial letter is always lower case.
No spaces should exist between a method name and the opening parenthesis of a parameter
list.
Proper method name examples:
getX()
createX(x)
updateMyTable()

STD-4 Variable naming.
Variable names should follow those of methods. Do not begin variable names with dollar
signs ($) although the compiler will permit this.
Proper variable name examples:
myVariable

STD-5 Constant naming.
In order to make constants stand out in the code they are named with all capital letters. In
order in increase the readability of the names, separate embedded words with a single
underscore.
Proper constant name examples:
MY_CONSTANT_A
INCHES_TO_CENTIMETERS_FACTOR

STD-6 Use of JavaDoc comments is required.
Because the public interface to a class is key to being able to use the class effectively,
JavaDoc comments are not optional for public classes and methods. The specific formatting
and minimum content may be open for discussion but the presence of JavaDoc comments is
not.
JavaDoc comments are important in that they provide documentation which is external to the
source code and which can be easily maintained.

STD-7 Use of implementation comments is required.
JavaDoc comments are intended to define and describe the public interface of a class or
method. Implementation comments are for describing the internal implementation.

STD-8 Consistency of formatting is required within a source file.
Sometimes you acquire source code which differs from the accepted format you usually
employ, including your choices for indentation, use of white space, etc. When this happens
resist the impulse to mix your personal adopted format with the existing one. Respect the
existing code and format your additions consistent with the existing format. Then pursue the
option to reformat the entire source file as a separate task.

STD-9 Avoid local declarations which obscure declarations at higher levels.
Do not declare a block variable with the same name as a method or class variable. Doing so
unnecessarily obscures the code.

AddThis Social Bookmark Button Bookmark this article with any Bookmarker (Digg, del.icio.us, Technorati, reddit, Yahoo MyWeb, Newsvine, Furl)