Saturday, December 27, 2014

Java Coding Standards

Introduction

We every one knows that, how to write the code to meet the requirement. But apart from the requirement many things need to be considered during software development. One of the important thing is Coding Standard.

Why Java Coding Standards?

Software industry is not like other industries to complete project at certain period of time. Software development will never end. Today you can develop a software, but that software should be maintained through out the software's life time. So the code what you have developed should be understandable by other programmers. The ultimate aim of this Coding Standard is to improve the quality of the Coding and more readability

Naming Conventions

Naming Conventions are important to makes programmers more understandable.
Below table describe the Naming Conventions,


Identifier Type Rules of Naming Conventions Example
Package Package name should be written in lower case. Prefix of the package should be any one the top domain name, like com, org, net, edu, gov. Sub package names can be vary based on the organizations standards. com.accounts.bean
com.utility.form
com.payments.action
Classes Class name should be noun. If your class name contains more than word, first letter of each word should be capitalized. Keep class name simple and meaningful. class TransactionManager
class Dog
Interfaces Interface name should be capitalized like class name interface MessagingService
interface Printing
Methods Method name should be verb. First letter of method should be small case. In case of more than one word in a method, each internal word should be capitalized. run();
play();
getConnection();
Variables Variable name should start with small case, in case of more than one word in a variable, each internal word should be capitalized. Variable should not start with underscore(_) or dollar sign($). Variable name should be short and meaningful. Avoid declare the variable with one word. One word variable can be used for temporary purpose. int rollNumber;
double amount;
char c;
Constants Constant name should be all upper case. In case of more than one word, each word should be separated with underscore(_). static final int MIN_LENGTH = 10;
static final int MAX_WIDTH = 100;


File Organization

  1. Creating a class or interface on right package is an import thing. Suppose if you are creating a class for the purpose Utility Payments. So that class should be placed under payments package. You should not place that class in Reports package
  2. Creating separate source file for each class and interface is the best practise. If there is any private class or interface which is associated with public class, you can put them in single source file.
  3. All source files should begin with a c-style comment that lists the class name, version information, date, and copyright notice:

    /*
    * Classname
    *
    * Version information
    *
    * Date
    *
    * Copyright notice
    */

Indentation

Indentation also an important thing in Java Coding Standards. Tab space can be used as a unit of Indentation. Avoid lines longer than 80 characters.
Statement which is more than 80 characters, that can be break with some general rules.

  1. Break after comma
  2. Break before operator
  3. Align the new line with the beginning of the expression at the same level on the previous line
  4. If breaking a Statement is leading to confusing code, tab space can be used

Comments

Comments should give the overview of the code. Comments should contains only the relevant information of a program.
Java program having tow types of comments
  1. Implementation Comment
  2. Java Documentation Comment
Implementation Comment
  1. Implementation comments are delimited with /*...*/ and //.
  2. Implementation comments describe about the particular implementation.
Java Documentation Comment
  1. Java doc comments describe the java classes, interfaces, methods, constructors and fields
  2. Java doc comments are delimited with /**...*/
  3. Java doc comment should be written just before the declaration.

Declarations

  1. Declare one variable per line is recommended
  2. Try to initialize the local variable at the time of declaration itself. This is may not possible all the time based on the requirement
  3. Declaration should be in beginning of the block
  4. Declare all the necessary variables at beginning of the block itself, don't wait to declare the variables until its first usage. (for loop index variable declaration is an exceptional case)
  5. No space between a method name and the parenthesis "("
  6. Open brace "{" at end of the same line in declaration statement
  7. Closing brace "}" starts a line by itself indented to match its corresponding opening statement

White Space

Blank Lines

Blank lines improve the readability of the code, Blank Lines can be used below circumstance

  1. Two Blank lines can be used between the section of a source file
  2. Two Blank lines can be used between the class and interface declaration
  3. One Blank line can be used between the methods
  4. One Blank line can be used between local variables of method and its first statement
Blank Spaces

Blank Spaces can be used below circumstance

  1. A keyword followed by parenthesis should be separated by a space
  2. A blank space should be used after comma in argument list
  3. All binary operands except dot(.) should be separated from their operands by a space. Blank space should not separate the unary operators

Java Coding Standard tools

Below open source tools can be integrated with Net Beans IDE for Java Coding Standards
  1. Checkstyle java standard tool
  2. PMD java standard tool

No comments:

Post a Comment