Using Bad Versus Good Variables and Function Names
Compare the two programs
below:
Example
1: The programmer has chosen to use poor
variable names.
def main():
# Open the sales.txt file for reading.
s = open('sales.txt',
'r')
# Read all the
lines from the file.
# Convert line to a
float.
a = float(l)
# Format and display the amount.
print(format(a,
'.2f'))
# Close the file.
s.close()
#
Call the main function.
main()
Example
2: The programmer has chosen to use good
variable names.
def main():
# Open the sales.txt file for reading.
sales_file_in = open('sales.txt',
'r')
# Read all the
lines from the file.
for i in sales_file_in:
# Convert line to a
float.
number =
float(i)
# Format and display the amount.
print(format(number,
'.2f'))
# Close the file.
sales_file_in.close()
#
Call the main function.
main()
In
example 2 above, the variable names are descriptive of the information they
hold, such as " sales_file_in" will hold data from the
"sales.txt" file. Also, notice the poor choice of variables in
Example 1 on line 6. " for l in s:" As you
can see, it is difficult if not impossible to tell that is the variable lower
case "L". Will likely lead to much
confusion.