Use consistent naming convention in programming to easily understand the source code

2 minutes read

In computer programming, naming convention might be considered as a set of rules for choosing the identifiers to denote variables, types and functions & methods names in source code and project documentation. The purpose for using this naming convention is only to reduce the effort needed to read and understand the source code and to also to enhance the code appearance by using appropriate and concise keys.

The main benefit of adopting naming convention is that one has not to re-trace the source code from top to bottom to understand the code done by any other programmer. For example, although the following:

a = b * c * d;

is syntactically correct, but its purpose is not evident. To understand this, one has to retrieve the whole source code.

 

Contrast this with:

monthly_pay = hours_worked * pay_rate * no_days;

which implies the intent and meaning of the source code, at least to those familiar with the underlying context of the application.

 

Some basic rules to follow naming convention:

 

1. Length of identifiers must be moderate: Too long identifiers are not good enough for presentation and too short such as “I”, ”j” are too much difficult to uniquely distinguishable for what purpose it has been used.

2. Letter case and numerals: Identifiers for parameters and variables must be lowercase.

3. Multiple word identifiers: For using multiple word identifiers, one can follow one of the two ways.

4. Delimiter separated keywords: One approach is to delimit separate words with a non-alphanumeric character. The two characters commonly used for this purpose are the hyphen (“-“) and the underscore (“_”); e.g., the two-word name “two words” would be represented as “two-words” or “two_words”.

5. Letter case separated keywords: Another approach is to indicate word boundaries using medial capitalization, thus rendering “two words” as either “twoWords” or “TwoWords”. This is also called Camel Case.

This naming convention adaptation also varies from one programming language to another. Such as if we consider C and Pascal, in this language “-“ is reserved for subtraction infix operator. One can’t use it in identifier naming convention.

 

Related Posts...

GeneralPHPTechnologiesWeb Design