1. Use full English descriptions for names. Avoid using abbreviations. For example, use names like firstName,lastName, and middleInitial rather than the shorter versions fName, lName, and mi.
2. Avoid overly long names (greater than 15 characters). For example, setTheLengthField should be shortened tosetLength.
3. Avoid names that are very similar or differ only in case. For example, avoid using the names product, products, and Products in the same program for fear of mixing them up.
For Ex : -
double tax1; // sales tax rate (example of poor variable name)
double tax2; // income tax rate (example of poor variable name)
double salesTaxRate; //no comments required due to
double incomeTaxRate; //self-documenting variable names
Variable Naming Conventions :-
Choose meaningful names that describe what the variable is being used for. Avoid generic names like number or temp whose purpose is unclear. Compose variable names using mixed case letters starting with a lower case letter. For example, use salesOrder rather than SalesOrder or sales_order.
Use plural names for arrays. For example, use testScores instead of testScore. Exception: for loop counter variables are often named simply i, j, or k, and declared local to the for loop whenever possible.
for (int i = 0; i < MAX_TEMPERATURE; i++){
boilingPoint = boilingPoint + 1;
}
Constant Naming Conventions :-
Use ALL_UPPER_CASE for your named constants, separating words with the underscore character. For example, use TAX_RATE rather than taxRate or TAXRATE. Avoid using magic numbers in the code. Magic numbers are actual numbers like 27 that appear in the code that require the reader to figure out what 27 is being used for. Consider using named constants for any number other than 0 and 1.
Method Naming Conventions :-
1.Compose method names using mixed case letters, beginning with a lower case letter and starting each subsequent word with an upper case letter.
2.Begin method names with a strong action verb (for example, deposit). If the verb is not descriptive enough by itself, include a noun (for example, addInterest). Add adjectives if necessary to clarify the noun (for example, convertToEuroDollars).
3.Use the prefixes get and set for getter and setter methods. Getter methods merely return the value of a instance variable; setter methods change the value of a instance variable. For example, use the method names getBalance and setBalance to access or change the instance variable balance.
4.If the method returns a boolean value, use is or has as the prefix for the method name. For example, use isOverdrawn or hasCreditLeft for methods that return true or false values. Avoid the use of the word not in the boolean method name, use the ! operator instead.For example, use !isOverdrawn instead of isNotOverdrawn.
Parameter Naming Conventions :-
With formal parameter names, follow the same naming conventions as with variables, i.e. use mixed case, begin with a lower case letter, and begin each subsequent word with an upper-case letter.
1.Consider using the prefix a or an with parameter names. This helps make the parameter distinguishable from local and instance variables. Occasionally, with very general purpose methods, the names chosen may be rather generic (for example, aNumber). However, most of the time the parameter names should succinctly describe the type of value being passed into the method.
Example:-
public void deposit(long anAccountNumber, double aDepositAmount)
{
...
}
public boolean isNumberEven(int aValue)
{
...
}
Read More !