Include Units in Variable Names [VF1]

Include Units in Variable Names [VF1]

Tip VF1

Unit mismatch errors can be catastrophic. Include units in variable names to make it easier to prevent such errors.

Code often has to do numeric calculations, and we have often have many different ways of measuring things with different units in the real world.

Improperly mixing units in mathematical calculations can be catastrophic. The Mars Climate Orbiter infamously missed its intended trajectory due to a mismatch of units, resulting in loss of an expensive spacecraft.

In programming languages with strict type checking, it may be possible to mitigate some of these issues with custom data types for different kinds of units. However, even in such programming environments, there can be benefits of directly embedding the units in the variable names.

A benefit of embedding units in variable names like this is that they are obviously available anywhere the variable is used. You can read the code, one line at a time, to verify correctness. By placing all of the relevant information as close as possible (in the same variable name) in this case, that makes it easier for a reader to reason about correctness and increases the chances that bugs will be spotted.

 

Tap into your teams collective wisdom ad.png