Naming Variables  

Russ Cox writes:

A name’s length should not exceed its information content. For a local variable, the name i conveys as much information as index or idx and is quicker to read. Similarly, i and j are a better pair of names for index variables than i1 and i2 (or, worse, index1 and index2), because they are easier to tell apart when skimming the program. Global names must convey relatively more information, because they appear in a larger variety of contexts. Even so, a short, precise name can say more than a long-winded one: compare acquire and take_ownership. Make every name tell.

The information content metric gives a quantitative argument against long-winded names: they’re simply inefficient. I internalized this metric years ago but only realized this phrasing of it recently, perhaps because I have been looking at too much Java code.

Yes, x sucks, but maybe I should stop writing tweet-sized variable names?

 
3
Kudos
 
3
Kudos

Now read this

I am the 2%

It is often difficult to express the feelings of discouragement, detachment, isolation, and self-doubt that come with being the anomaly of the tech industry. After a career of 4+ years at giant Silicon Valley tech companies, where racial... Continue →