Vlad's Roam Garden

Powered by 🌱Roam Garden

Use Long Names for Long Scopes

The length of a name should be related to the length of the scope. You can use very short variable names for tiny scopes, but for big scopes you should use longer names.

In computational terms - the name of the entity is the key you'd use to find it within its scope. If the scope is small - a shorter name is sufficient to identify the entity unambiguously. With the larger scope - you have a large number of elements, and so the entity name needs to contain more information.

A related thought is to strive to keep any given scope small - reduce the number of things you need to be aware of to understand any given code block

The length of a name should be related to the length of the scope. You can use very short variable names for tiny scopes, but for big scopes you should use longer names.
Variable names like i and j are just fine if their scope is five lines long. Consider this snippet from the old standard “Bowling Game”:
  for (int i=0; i<n; i++)  g.roll(pins);  
}
This is perfectly clear and would be obfuscated if the variable i were replaced with something annoying like rollCount. On the other hand, variables and functions with short names lose their meaning over long distances. So the longer the scope of the name, the longer and more precise the name should be