Chapter 2 - Packing Information into Names

Published:

Packing Information into Names

Choose specific words

def GetPage(url) doesn’t say much. Maybe FetchPage() or DownloadPage() is more specific?

int Size() - is this size the height of an object, numbeer of elements or memory footprint? Maybe Height(), NumNodes() or MemoryBytes() is better.

A thesaurus can be useful.

send -> deliver, dispatch, announce, distribute, route find -> search, extract, locate, recover start -> launch, create, begin, open make -> create, set up, build, generate, compose, add, new

Avoid Generic Names Like tmp and retval

tmp and retval names don’t pack much meaning; use names that describe the variable’s value.

tmp is suitable where its short-lived and its important to indicate that the variable is indeed temporary.

Attaching Extra Information to a Name

If variable is a measurement, add units as a suffix. This is exactly what Prometheus recommends for metric naming as well.

If you’re dealing with data that needs to be cleaned before use, add this information to the name. E.g. comment -> unescaped_comment, data -> data_urlenc.

Short names are good for short scope. The names of global variables should carry enough information to make it clear what they are.

As for abbreviations, e.g. str, doc, eval are OK as most people are familiar with them. But BEManager isn’t clear that it means BackEndManager.

Likewise, unneeded words should be removed. Don’t say ConvertToString(), ToStr() is clear enough.

Use Name Formatting to Convey Meaning

Different formats for different entities.

CamelCase for class names. lower_separated for variable names. kConstantName for constants. offset_ for class member variables. MACRO_NAME for macros. DatePicker() for constructor functions (new DatePicker()). pageHeight() for ordinary functions.