Measure Complexity to Avoid Software Bugs

Measure Complexity to Avoid Software Bugs

Apr 28, 2015 Blog by colinalb

I have posted this to remind myself of how important it is measure and minimise code complexity. These truisms all coincide:

  • Defects can be avoided with good code structure
  • Most serious defects are found in the most complex routines
  • Prevention is better than cure

If you measure your McCabe Complexity throughout your code development and make sure that no routines exceed an MCC value of 10, then your software stands a good chance of being both easy to maintain and have a low defect density.

There are many free tools out there, I have recently been using PHPMD and found it to be excellent.

Nothing to do!
Run, refactor, repeat
prompt> php phpmd.phar /path-to-code-directory text codesize

Remember to write thorough unit tests for any code that approaches an MCC of 10.