Monday, August 9, 2010

Naming convention of Variable

Variables:

Variables should be initialized where they are declared and they should be declared in the smallest scope possible.


This ensures that variables are valid at any time. Sometimes it is impossible to initialize a variable to a valid value where it is declared. In these cases it should be left uninitialized rather than initialized to some phony value.


Variables must never have dual meaning.


Enhances readability by ensuring all concepts are represented uniquely. Reduce chance of error by side effects.

C

lass variables should never be declared public.


The concept of Java information hiding and encapsulation is violated by public variables. Use private variables and access functions instead. One exception to this rule is when the class is essentially a data structure, with no behavior (equivalent to a C++ struct). In this case it is appropriate to make the class' instance variables public.


Arrays should be declared with their brackets next to the type.

double[] vertex; // NOT: double vertex[]; int[] count; // NOT: int count[]; public static void main(String[] arguments) public double[] computeVertex()


The reason for is twofold. First, the array-ness is a feature of the class, not the variable. Second, when returning an array from a method, it is not possible to have the brackets with other than the type (as shown in the last example).


Variables should be kept alive for as short a time as possible.


Keeping the operations on a variable within a small scope, it is easier to control the effects and side effects of the variable.

Variables:

Variables should be initialized where they are declared and they should be declared in the smallest scope possible.


This ensures that variables are valid at any time. Sometimes it is impossible to initialize a variable to a valid value where it is declared. In these cases it should be left uninitialized rather than initialized to some phony value.


Variables must never have dual meaning.


Enhances readability by ensuring all concepts are represented uniquely. Reduce chance of error by side effects.

Class variables should never be declared public.


The concept of Java information hiding and encapsulation is violated by public variables. Use private variables and access functions instead. One exception to this rule is when the class is essentially a data structure, with no behavior (equivalent to a C++ struct). In this case it is appropriate to make the class' instance variables public.


Arrays should be declared with their brackets next to the type.

double[] vertex; // NOT: double vertex[]; int[] count; // NOT: int count[]; public static void main(String[] arguments) public double[] computeVertex()


The reason for is twofold. First, the array-ness is a feature of the class, not the variable. Second, when returning an array from a method, it is not possible to have the brackets with other than the type (as shown in the last example).


Variables should be kept alive for as short a time as possible.


Keeping the operations on a variable within a small scope, it is easier to control the effects and side effects of the variable.