Following is a quick way to identify code with low testability and hence low maintainability:
- Take any specific public method of the class
- Count for number of decision points such as following:
– If
– For
– While
– Switch
– Inline ifs
- If the number of decision points in a method is more than 15 or so, it could be taken as indication that it may get difficult to write unit tests covering all possible flows. Thus, the code will have lower testability.
The number of decision points in a method is also used to define/measure cyclomatic complexity of the method, or more specifically, McCabe Cyclomatic Complexity. Thus, mathematically speaking, testability can be represented as following:
Testability, T of a method is inversely proportional to No. of Decision Points, D, found in the method.
Latest posts by Ajitesh Kumar (see all)
- A Comprehensive List of Agentic AI Resources - January 5, 2025
- Understanding FAR, FRR, and EER in Auth Systems - January 3, 2025
- Top 10 Gartner Technology Trends for 2025 - January 1, 2025
I found it very helpful. However the differences are not too understandable for me