Temporary/Intermediate Data [VD4]
Tip VD4
Give clear and accurate names to “temporary” or “intermediate” variables. Doing so will help ensure both you and future programmers understand the problem and the code trying to solve it.
If you’ve been following our guidance to break up complex expressions into smaller chunks assigned to intermediate variables, you’ll likely have many intermediate variables. Naming those intermediate variables can be tricky, particularly if you’re translating something like a mathematical formula from a different source. Such “intermediate” or “temporary” variables can also occur if you are building up some value that is a “work in-progress” over a function.
It can be tempting to give such variables names like temp
. Be wary of such names. Beyond not communicating much to a reader, a key reason to avoid such names is that they’re a warning sign that you don’t fully understand the problem. If you don’t fully understand the problem your code is trying to solve, how can you be sure it is solving it correctly? In fact, while it may not always be possible or make the most sense to create intermediate variables for every small subexpression, avoiding creation of an intermediate variable due to an inability to think of a good name is another warning sign of lack of understanding. Taking the time to understand the code well enough to craft a meaningful variable name is a worthwhile investment in making sure your code is correct.
Consider the following example: