Good coding practices- Hard coding and variables
As we go through this tutorial, I will try to make a point to emphasize certain “good coding practices”. In our first program, I’ve tried to use two such practices. First, to avoid hard coding and second using good names for your variables.
Hard Coding
Generally, hard coding is the practice of writing code without using variables appropriately. For example, we executed the following lines of code in the previous section:
[ ]:
nSecondsInDay = 24 * 60 * 60
print(nSecondsInDay)
nSecondsInYear = nSecondsInDay * 365.25
print(nSecondsInYear)
I could have written the lines above in a different way and achieved the same result:
[ ]:
print(24 * 60 * 60)
nSecondsInYear = 24 * 60 * 60 * 325.25
print(nSecondsInYear)
In second version, I printed the number of seconds in a day and then calculated the number of seconds in a year by performing the same calculation that I used in the previous line. The first version made use of the variable nSecondsInDay and thus avoided having multiple instances of the same calculation. Not only is this more efficient to code in the first place and more efficient for the computer itself, it is also easier to alter the code in the first example. Let’s say I used this code at
a planet where there were 25 hours per day instead of 24, in the first example, I would only have to alter the code in a single location as opposed to the 2 locations in the second example.
Variable names
Using good variable names should be obvious. Note that in the examples provided so far, I named my variables in such a way that they told me what they represent physically. Naturally, this practice makes it much easier to understand the code that you wrote. Not only will this help other users of your code, but your future self will thank you as well. Save single character variable names for counters, and that’s about it.
Example- bad variable names:
[ ]:
t = 273.15
or
[ ]:
temp = 273.15
The variable t is hardly useful at all as we would have very little idea what that represents. Similarly temp could mean many things (and is often a good way to say “temporary”.
Instead, a good variable name is:
[ ]:
temperature = 273.15
[ ]:
temperatureCelcius = 273.15
It is pretty clear that these values are temperatures. In the latter case, we even include the unit so there is little ambiguity in the information that should be held by that variable.
Of course, there is some trade off between a good, descriptive variable name, and something that is so long it becomes a pain to use:
[ ]:
temperaturecelciusduringthatonedaythaticareaboutalongtimeago = 273.15
The point of variables is to use them multiple times when performing calculations, so using a variable name that is very long can become cumbersome.
Variable name formatting
There are couple common options for formatting variable names. In python, it is popular to use camelCase, where the first letter of each word in a variable name is capitalized, with the exception of the first word:
[ ]:
numberSecondsInDay = 86400
Alternatively, some people prefer to pothole_case where underscores are used in place of spaces between different words:
[ ]:
number_seconds_in_day = 86400
Other options abound but the important thing is to use descriptive variable names that are readable and to use a consistent style.
There are several python keywords that can’t be used as variable names. A few common examples: for, True, False, and, if, int all have special meaning in python and will cause an error if you try to assign a value to them.