Vlad's Roam Garden

Powered by 🌱Roam Garden

Use explanatory variables

In computational terms you can think of this as using caching.

The variable with a good names allows you to retrieve the meaning of the statement a lot faster then if you have to mentally parse it yourself.

Example::

val 

More names means more domain information

We find that when we emphasize how objects communicate, rather than what they are, we end up with types and roles defined more in terms of the domain than of the implementation. This might be because we have a greater number of smaller abstractions, which gets us further away from the underlying language. Somehow we seem to get more domain vocabulary into the code.

Kent Beck wrote about this in his great book Smalltalk Best Practice Patterns and again more recently in his equally great book Implementation Patterns. One of the more powerful ways to make a program readable is to break the calculations up into intermediate values that are held in variables with meaningful names.
Consider this example from FitNesse:
if(match.find())  { 
  String key = match.group(1);  
  String value = match.group(2);  
  headers.put(key.toLowerCase(), value);  
}
The simple use of explanatory variables makes it clear that the first matched group is the key, and the second matched group is the value.
It is hard to overdo this. More explanatory variables are generally better than fewer. It is remarkable how an opaque module can suddenly become transparent simply by breaking the calculations up into well-named intermediate values.