Wednesday, 19 February 2014

JCode Standard Format

Java Code Standard
1) Beginning Comments

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

2) Package And Import Statement.


package java.awt;
 
import java.awt.peer.CanvasPeer;

4) Line Length
Line length shouldn’t be more than 80 characters. Even for the comments.
5) Line Wrapping Rules
Ø  Break after a comma.
Ø  Break before an operator.
Ø  Prefer higher-level breaks to lower-level breaks.
Ø  Align the new line with the beginning of the expression at the same level on the previous line.
Ø  If the above rules lead to confusing code or to code that's squished up against the right margin, just indent 8 spaces instead.
Examples:
i)     someMethod(longExpression1, longExpression2, longExpression3,
              longExpression4, longExpression5);

ii)     Preferred Way:
longName1 = longName2 * (longName3 + longName4 - longName5)
            + 4 * longname6;

Wrong Way:
longName1 = longName2 * (longName3 + longName4
                      - longName5) + 4 * longname6;

iii)    //CONVENTIONAL INDENTATION
 
Preferred Way:
someMethod(int anArg, Object anotherArg, String yetAnotherArg,
           Object andStillAnother) {
    ...
}
          Wrong Way:
  //INDENT 8 SPACES TO AVOID VERY DEEP INDENTS
private static synchronized horkingLongMethodName(int anArg,
        Object anotherArg, String yetAnotherArg,
        Object andStillAnother) {
    ...
}
iv)    //DON'T USE THIS INDENTATION

Wrong Way:

if ((condition1 && condition2)
  || (condition3 && condition4)
  ||!(condition5 && condition6)) { //BAD WRAPS
  doSomethingAboutIt();            //MAKE THIS LINE EASY TO MISS
}

Preferred Way:

        //USE THIS INDENTATION INSTEAD

if ((condition1 && condition2)
      || (condition3 && condition4)
                ||!(condition5 && condition6)) {
             doSomethingAboutIt();
}

       //OR USE THIS

         if ((condition1 && condition2) || (condition3 && condition4)
        ||!(condition5 && condition6)) {
  doSomethingAboutIt();
}

6) Type Of comments
   i) Implementation Comment { eg: /*…*/}
      comments about the implementation of code.
   ii) Documentation Comment { eg: /** …*/}
      description of code specification , because the developer  may or may not have source code.
7) Provide a link in the comment.
 /*
   *{@link image_link}
   */
8) We can also use HTML meta-character such as "&" or "<"  inside the comment eg:
Prof. Knuth's MIX è Prof.&nbsp;Knuth's
 
9) @deprecate will be the first line, and through which we can say that the newest version of functionality of deprecated function.
 
/**
 * @deprecated As of JDK 1.1, replaced by
 *              {@link #setBounds(int,int,int,int)}
 */
 
9) Variable declaration ways
 
Wrong Way: int level, size;
 
Preferred Way:  int level;
                Int size;
10)  Always initialize the local variable.
 
11)  Always declare variable only at the beginning of the block.
 
void myMethod() {
    int int1 = 0;         // beginning of method block

    if (condition) {
        int int2 = 0;     // beginning of "if" block
        ...
    }
}
 
12) Naming Convention

Naming conventions make programs more understandable by making them easier to read. They can also give information about the function of the identifier-for example, whether it's a constant, package, or class-which can be helpful in understanding the code.

Identifier Type

Rules for Naming

Examples

Packages
The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981.
Subsequent components of the package name vary according to an organization's own internal naming conventions. Such conventions might specify that certain directory name components be division, department, project, machine, or login names.
com.sun.eng
com.apple.quicktime.v2
edu.cmu.cs.bovik.cheese
Classes
Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).
class Raster;  class ImageSprite;
Interfaces
Interface names should be capitalized like class names.
interface RasterDelegate;  interface Storing;
Methods
Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.
run();  runFast();  getBackground();
Variables
Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.
Variable names should be short yet meaningful. The choice of a variable name should be mnemonic- that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary "throwaway" variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.
int             i;
char            c;
float           myWidth;
Constants
The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores ("_"). (ANSI constants should be avoided, for ease of debugging.)
static final int MIN_WIDTH = 4;
static final int MAX_WIDTH = 999;
static final int GET_THE_CPU = 1;
13) Overall Source File Code Style
 
/*
 * @(#)SampleCode.java        1.82 99/03/18
 * --- your company copyright description ---
 */
 
 
package java.code.format.sample;
 
import java.text.SimpleDateFormate;
 
/**
         Class description goes here.
 *
 * @version  1.82
 * 18 Mar 1999  
 * @author Firstname Lastname  
 */
 
public class SampleCode extends SomeClass {
  
    /* A class implementation comment can go here. */ 
 
    /*  
     * classVar1 documentation comment 
     */
 
    public static int classVar1;
 
    /*
     * classVar2 documentation comment that happens to be
     * more than one line long
     *  
     */
 
    private static Object classVar2;
 
    /** instanceVar1 documentation comment */
 
    public Object instanceVar1;
 
    /** instanceVar2 documentation comment */
    protected int instanceVar2;
 
 
    /**  instanceVar3 documentation comment */
    private Object[] instanceVar3;
 
 
    /** 
     * ... constructor Blah documentation comment...      
     */
    public Blah() {
  
               // ...implementation goes here...     
    }
 
    /**
     * ... method doSomething documentation comment...      
     */
    public void doSomething() {
  
                // ...implementation goes here...      
    }
 
    /**
     * ...method doSomethingElse  documentation comment...      
     * @param someParam  description      
     */
 
    public void doSomethingElse(Object someParam) {
  
                // ...implementation goes here...      
    }
 
    /**
     * ...method doSomethingElse  documentation comment...      
     * @param someParam  description.      
     * @return description.
     */
 
    public Object returnSomething(Object someParam) {
  
                // ...implementation goes here...      
    }
 
 
}