Cognitive complexity

Cognitive complexity image
Cognitive complexity is a software quality metric that quantifies the mental effort required to understand code. The cognitive complexity meaning refers to how difficult it is for an engineer to read, comprehend and work with a piece of code.

The cognitive complexity function encompasses several key elements. Cognitive complexity starts with a base score of zero and adds points for specific code patterns: decision points, nested level, flow interruption, recursion, and logical operators. The total score reflects how challenging the code is for a human to follow. For example, a simple if statement adds 1 point. That same if statement nested inside a loop adds 2 points (1 for the condition, +1 for nesting). A nested if with multiple logical operators could add 3-4 points.

High cognitive complexity slows development, increases bugs and makes code harder to maintain.

Cognitive complexity is one of the most overlooked barriers to developer productivity. When code becomes difficult to understand, it slows down every part of the development process: debugging takes longer, modifications are more likely to introduce bugs and onboarding new developers becomes a costly exercise in confusion. This also applies to testers working on test automation: debugging test failures takes longer, modifications are more likely to introduce complexity, duplication or code that does not represent the desired behaviour of the system under test and onboarding new people to work on the automated tests becomes a costly exercise.
Explore MoT
RiskStorming: Artificial Intelligence image
RiskStorming; Artificial Intelligence is a strategy tool that helps your team to not only identify high value risks, but also set up a plan on how to deal
MoT Software Testing Essentials Certificate image
Boost your career in software testing with the MoT Software Testing Essentials Certificate. Learn essential skills, from basic testing techniques to advanced risk analysis, crafted by industry experts.
This Week in Quality image
Debrief the week in Quality via a community radio show hosted by Simon Tomes and members of the community
Subscribe to our newsletter
We'll keep you up to date on all the testing trends.