Want to Improve Code Quality? Focus on Cognitive Complexity.

The overlooked metric that is crucial to crafting quality code.

thecoderhaven
GEEKS FOR TECH

--

Photo by Gaspar Uhas on Unsplash

We’ve all, to some extent, measured the time it takes to run code and the space our code occupies in memory, but how often have we measured how easily understood our code is by the human mind? Probably not as much.

However, higher-level programming languages were created because they’d be understandable to humans. Logically, the understandability of our code is then crucial. But how do we go about measuring such a thing?

G. Ann Campbell of SonarSource has proposed a way of measuring a code’s understandability through a measure she calls cognitive complexity.

Measuring Cognitive Complexity

Code can become hard to intuitively understand for several reasons. The main reasons highlighted by Cambell are as follows:

  1. There are breaks in the “linear flow of code”
  2. “Flow breaking structures” are nested

We’ll look into what each of these points means in just a bit.

Before we do, we should note that according to Campbell’s definitions, code is not to be considered more (or less) complex for using shorthand commands that a…

--

--

thecoderhaven
GEEKS FOR TECH

@thecoderhaven publishes articles on a variety of tech-related topics. Jokes, coding tutorials, news insights, helpful advice and more are frequently posted.