Keep the work inside a method at a consistent level of abstraction: each operation should be expressed at a similar conceptual scale. Mixing a detailed loop with a high-level method call can make the method harder to read. section 2.5.3
public String evaluate() {
String result = templateText;
for (Entry<String, String> entry : variables.entrySet()) {
String regex = "\\$\\{" + entry.getKey() + "\\}";
result = result.replaceAll(regex, entry.getValue());
}
checkForMissingValues(result);
return result;
}
This evaluator replaces variables in a loop, then checks for missing values with a method call—a mismatch in abstraction level. section 2.5.3
Refactoring the evaluator
Extracting variable replacement into its own method makes evaluate more balanced. Rerun the tests to check that the refactoring preserves behavior. section 2.5.3
