Skip to content

Files

Latest commit

 

History

History
25 lines (16 loc) · 1.48 KB

CognitiveComplexMethod.md

File metadata and controls

25 lines (16 loc) · 1.48 KB

Pattern: High cognitive complexity

Issue: -

Description

Complex methods are hard to understand and read. It might not be obvious what side-effects a complex method has.

Prefer splitting up complex methods into smaller methods that are in turn easier to understand. Smaller methods can also be named much clearer which leads to improved readability of the code.

This rule measures and restricts the complexity of the method through the Cognitive Complexity metric of Sonasource. Which improves McCabe's Cyclomatic Complexity (see CyclomaticComplexMethod) considering the programmer's mental model.

Similar to cyclomatic complexity, it is a mathematical model that increases +1 complexity for flow control statements, but increases additional complexity when the statements are deeply nested.

The statements that increase the complexity or the nesting level are as follows.

  • Complexity Increments - if, when, for, while, do while, catch, labeled break, labeled continue, labeled return, recursion call, &&, ||
  • Nesting Level Increments - if, when, for, while, do while, catch, nested function
  • Additional Complexity Increments by Nesting Level - if, when, for, while, do while, catch

Further Reading