by Leon Rosenshein

Naming Things

There are 2 hard problems in computer science, cache invalidation, naming things, and off-by-one errors. I'm going to talk about the middle one.

Names are important. At the simplest, they are identifiers that are (hopefully) easy to remember. But they are more than that. They set context. They inform metaphors. They can guide you in the right direction. Good names also respect and help manage scope.

Scope of influence is a good place to start with naming. Variables defined inside the scope of a method have that context, so you can use that to your advantage when choosing a name. Method names usually exist in the context of a class/package/namespace, so use that as well.

Consider the lowly row. If you call something a row what is it? If you do it inside the context of a database iterator then there's a good chance it's a row in a database table. On the other hand, if you're processing an image the row is probably a line of pixels. When working on a spreadsheet it's straightforward as well. In the read method in an vehcle_model class in a fleet management database library you don't need to call a variable `row_of_vehicle_models_for_fleet_management`, just call it `row`. On the other hand, if you're working on a translator from the database to some over the wire JSON then you might want to have `database_model_row` and `json_model_row` instead of `row1` and `row2`.

At a higher level, names become metaphors and help guide your thoughts in the right direction. For example, in a UI you might have check boxes and radio buttons. Visually they're pretty similar. When selected they have a mark, when deselected they don't. The metaphor helps because it lets you know how they're used. Pick as many checkboxes as you want. With a set group of radio buttons you can only have one selected at a time.

At an even higher level, how do you name your team/project/toolset? I worked on a windows installer builder called Wix. When it started it was Windows Installer eXtensions, and didn't do much. Over the years a whole ecosystem of tools has grown up around it and the names all relate to "wicks". There's candle, light, dark, smoke, and a host of others. All named around the candle metaphor. It helps to group logical things together, which reduces the cognitive load.

So, regardless of your scope, think about naming. It will make a difference.