Use Descriptive Names#
Note
This notebook is meant to be used interactively. Use the “Live Code” button under the Rocket Ship icon in the top right to activate the page and use Python interactively!
My colleague/student constantly shares code with me but the variables are acronyms and are overwritten, which makes it difficult to read and use.
X1, X2, X3… oh, again X1 (overwrite). Was the previous X1 important?
Often when you work, you will be sharing your code with other people and you need make sure that they understand what each variable is doing and how it relates to the other variables. However, this may difficult or even impossible when the variable names do not represent the purpose they have.
For example, variable names such as a
, a1
, x
, plot
, plot1
may not be descriptive enough to use in your code. Additionally, kruidnoten
may not be a good variable name for a weather time series plot.
Coming up with descriptive names requires practice, but when in doubt don’t be afraid to use longer names to be specific. It’s easy to change names in a script later with find and replace. The only downside if your names are too long is that you have to add a few more line breaks (we give you a few tips for this in Golden Role), however, the downside of names that are too short is that you or a colleague has to spend hours in the future interpreting, or even rewriting your code.
However, there are some names to avoid. Namely, never use the characters l
(lowercase letter el), O
(uppercase letter oh), or I
(uppercase letter eye) as single character variable names. In some fonts, these characters are indistinguishable from the numerals one and zero. When tempted to use l
, use L
instead. Better yet: consider a variable name that is longer than one character.
Function names should be lowercase, with words separated by underscores as necessary to improve readability:
this_is_a_properly_formatted_function_name_its_too_long_but_very_readable
Variable names follow the same convention as function names. We will cover some of the other naming guidelines in later tutorials. For now, remember: functions and variables should be written in all lower case and underscores. That makes it easy to remember! For everything else, you can refer to PEP 8.