0

A common advice on method/function length is to write small methods to increase both readability and testability. These are measured, for example, by cyclomatic and cognitive complexity metrics. Similar metrics apply to classes, or in general, whatever code units the programming language is using.

However, applying these steps yields many small methods that are only readable and testable by themselves, but losing the "big picture". To understand the code, one doesn't have to understand, say, a single 200-line method anymore, but 30 10-line methods (by my experience, the total LOC usually grows a lot). Worse, the reader has to "jump around" between them a lot. To actually understand the code, the reader has to hold much more information in his mind than before the split. The same problem re-appears when trying to write an integration test that tests these methods together.

This leads to a situation in code reviews where single-method complexity metrics are applied without any counter-balance and actually degrading readability and testability in the name of "clean code".

Are there good metrics for readability and testability for multiple methods taken together that can be used to counter-balance cyclomatic and cognitive complexity metrics when they are applied, for example, in code reviews?

Martin Geisse
  • 1,189
  • 1
  • 9
  • 22

0 Answers0