2.1 Apply meaningful names to variables and functions
Due to the complex nature of computer programming, it is important that designers and programmers make their designs and resulting programs as readable as possible. This aids not only in the development of large computer programs but in subsequent maintenance.
Typically, an organization with a programming department will draw up and abide by a organisational naming convention. However, even for smaller organizations and programmers in general, it is advantageous to adhere to the following rules of thumb with selecting names for variables, constants and functions.
Choose and stick by a naming standard, eg. the decision of which type of casing to adopt.
a) All lower case – grosspay
b) A mixture of upper and lower case – GrossPay
c) The use of a separator – Gross_Pay or gross_pay
d) The use of camel casing – grossPay
It may be useful to use a verb/noun combination for the description of functions. The verb should give an idea of what the function will do and the noun the item upon which the operation will take place, eg. function calculate_netpay ( )
Advantages of adopting a meaningful naming convention:
- Reduce errors
- Avoidance of duplication errors
x = y * z;
The above code may produce no syntax errors, however, in terms of readability and understanding it could be a lot better. This code could be rewritten as shown below without affecting the arithmetic result:
gross_pay = hours_worked * hourly_rate;
Note: As well as rigorously commenting code, the use of meaningful names within a program will assist readability.