Clean Code: Smells and Heuristics

G19: Use Explanatory Variables

Kent Beck wrote about this in his great book Smalltalk Best Practice Patterns1 and again more recently in his equally great book Implementation Patterns2. 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 the following example from FitNesse:

  Matcher match = headerPattern.matcher(line);
  if(match.find()) {
    String key =;
    String value =;
    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.

  1. [Beck97], p. 108. 

  2. [Beck07].