Java Coding Standards Part 2

Some of the advance concepts of coding standards and formatting are:

Coding Standards in Java Bean

Java bean are the java classes with some private variables and public getter and setter methods. These classes are made for the purpose of transferring data from one class to another. Consider the example of Java bean with coding standards,

public class EmployeeBean                 
{
private String name;
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
}

From the above code you can deduce the following important points:

  1. Class name should end up with Bean.
  2. Class should be public.
  3. Every instance or global variable is kept private.
  4. Methods can either set the value or retrieve the value.
  5. Methods which sets the value of the variable is named as
    public void setVariableName(datatype variablename)
  6. Getter Methods are used for retrieving the values and should be named as
    public returntype getVariableName()
  7. this.name=namesets the value of instance variable to the value argument value of the setter method.

Note- For the variables with Boolean datatype get is replaced with is. For example,

private Boolean available;
public Boolean isAvailable(Boolean available)
{
return available;
}

Coding standards for Listeners

Listener methods are generally used to run a part of code when any event is fired or initiated. They are mostly used in GUI programming. To create a Listener , points that needs to be kept in mind are:

  • Prefix the method name with add.
  • After that give any name to the class.
  • Suffix the method name with Listener.
  • Name of the argument type should be same as the name of method without prefix and suffix.

For example,

public void addOnClickListener(OnClickListener o)            
//valid
public void registerOnClickListener(OnClickListener o)       
//invalid, because prefix add is not used
public void addOnClickListener(Listener o)                   
//invalid, because argument type is not same as method name

While un-register any listener class, one has to create the method same as above but with prefix remove rather than add. For example,

public void removeOnClickListener(OnClickListener o)
//valid
public void unregisterOnClickListener(OnClickListener o)
//invalid, method name should have prefix remove
public void deleteOnClickListener(OnClickListener o)
//invalid, method name should have prefix remove
public void removeOnClickListener(ClickListener o)
//invalid, method name should have prefix remove

Formatting Standards

Formatting standards in Java deals with the format in which programs should be written. Therefore it governs the use of braces, indentation, non empty blocks and more such things. Important point to remember in formatting are:

  • Braces should be used after the use of if, else, for, do while and while.
  • No line break before the opening brace.
  • Line break should also be there before closing brace.
  • There should be line break after the opening and the closing brace.
  • Empty blocks should be closed immediately with no line break.
  • Indentation after opening a block should be +2 spaces.
  • There should only be one statement in one line.
  • Any statement line should not exceed space of 100 character.
  • An array formatting should be in any manner such that is like block.
  • Comments should be done in block style.
  • Annotations should always be mentioned.

 

Leave a Comment

Your email address will not be published. Required fields are marked *