Handling cyclomatic complexity

Cyclomatic complexity is a measure of how many linearly independent paths there are through a program's code.

Consider a simple program that contains several conditional checks and function invocations:

if (a) { alpha(); if (b) bravo(); if (c) charlie();}if (d) delta();

Even in this misleadingly simple piece of code, nine distinct paths can be taken. So, depending on the values of a, b, c, and d, there are nine possible sequences of alpha, bravo, charlie, and delta that will run:

  • alpha()
  • alpha() and bravo()
  • alpha(), bravo(), and charlie()
  • alpha(), bravo(), charlie(), and delta()
  • alpha(), bravo(), and delta()
  • alpha() and charlie()
  • alpha(), charlie(), and delta()
  • alpha() and delta()
  • delta()

A high level of ...

Get Clean Code in JavaScript now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.