A Quick Way to Identify Code with Low Testability

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.

Ajitesh Kumar

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.
Posted in Maintainability, Software Quality, Unit Testing. Tagged with .