Thursday, November 8, 2012

Coding standards


1)      Do not use interfaces to define Constants.
2)      Change constants to Enumerated type wherever required.
3)      Avoid calling the same method at multiple instances in the same class especially. For such method calls, use instance variables and initialize them accordingly.
4)      From Wicket API front, when a piece of code gets executed (during compilation/runtime or while performing some operation) should be clearly understood like when :
è A particular method will be fired.
è Object creation happens.
è Variable initialization happens.
5)      Use appropriate naming conventions for members of the class and also wicket id’s.
6)      Local and instance variables should be used appropriately.
7)      No instance variable should have default access specifier, always declare relevant access specifier (public, private or protected).
8)      A boolean value can be associated with a method in any of the three ways :
è Set the boolean value “true” or “false” directly in the method based on the condition.
Ex: setEnabled(true);
è Initialize a boolean variable and update it based on the condition and finally set the variable to the method.
Ex: boolean isEnabled = true;
        If(condition) {
                        isEnabled = false;
}
setEnabled(isEnabled).
è Include the condition itself in the method.
Ex: setEnabled(condition);

                For the above mentioned functionality, the first method is appropriate as the other two methods have readability issues.
Which way should be followed is based on the Context and Readability which in turn improves the maintainability.
9)      Add TODO’s when any particular code needs to be removed/changed.
10)   Use appropriate setter and getter methods based on the variable type being used.
Ex: For boolean type use “is..()” method instead of “set..()” method.
11)   Do not use Wrapper instead of DataType when not required.
12)   Use the instance variables within a class directly instead of calling getter method to get the value of that variable.
13)   Mention the correct size while creating the collection objects, when we know that only a standard number of elements will be used with it. Do not mention the size if not required.
14)   Enums should be understood thoroughly and have to use them appropriately.
15)   Need to make sure that the coding is done in a consistent manner like using the same case for String literals, property file keys, etc.