3. CHOOSING UNDERSTANDABLE NAMES¶
“The two hardest problems in computer science are naming things, cache invali dation, and off-by-one errors.” This classic joke, attributed to Leon Bambrick and based on a quote by Phil Karlton, contains a kernel of truth: it’s hard to come up with good names, formally called identifiers, for variables, functions, classes, and any- thing else in programming. Concise, descriptive names are important for your program’s readability. But creating names is easier said than done. If you were moving to a new house, labeling all your moving boxes as “Stuff” would be concise but not descriptive. A descriptive name for a program- ming book might be Invent Your Own Computer Games with Python, but it’s not concise.
Unless you’re writing “throwaway” code that you don’t intend to main- tain after you run the program once, you should put some thought into selecting good names in your program. If you simply use a , b , and c for vari- able names, your future self will expend unnecessary effort to remember what these variables were initially used for.
Names are a subjective choice that you must make. An automated for- matting tool, such as Black, described in Chapter 3, can’t decide what you should name your variables. This chapter provides you with some guide- lines to help you choose suitable names and avoid poor names. As always, these guidelines aren’t written in stone: use your judgment to decide when to apply them to your code.
ME TA SY NTAC TIC VA RI A BLE S
We commonly use a metasyntactic variable in tutorials or code snippets when we need a generic variable name. In Python, we often name variables spam , eggs , bacon , and ham in code examples where the variable name isn’t important. That’s why this book uses these names in code examples; they aren’t meant for you to use them in real-world programs. These names come from Monty Python’s “Spam” sketch (https://en.wikipedia.org/wiki/Spam_(Monty_Python)). The names foo and bar are also common for metasyntactic variables. These are derived from FUBAR, the World War II era US Army slang acronym that indi- cates a situation is “[messed] up beyond all recognition.”