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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.