Metric-CognitiveComplexity

Cognitive Complexity

Required inputs: IR, RFG

Computes the cognitive complexity of routines based on its statement sequence.

The value of Metric.CognitiveComplexity is computed roughly by summing up

  • the number of if- and preprocessor if-statements,
  • the number of else if- and preprocessor elif-statements,
  • the number of else- and preprocessor else-statements,
  • the number of loop statements (e.g. for and while),
  • the number of conditional (?) operators,
  • the number of switch statements,
  • the number of catch clauses
  • the number of goto-statements
  • the number of binary logical operator changes in operator sequences, and
  • if the routine is in a static call cycle: the number of routines contained within the cycle.

Additionally a nesting modifier is added, which gets incremented for statements inside

  • if-statements or preprocessor if-statements,
  • else if- and preprocessor elif-statements,
  • else- and preprocessor else-statements,
  • loop statements (e.g. for and while),
  • conditional (?) operators,
  • switch statements,
  • catch clauses, and
  • lambda expressions.

The nesting modifier gets added to

  • if-statements or preprocessor if-statements,
  • loop statements (e.g. for and while),
  • conditional (?) operators,
  • switch statements, and
  • catch clauses.
Note: Include guards are ignored by this metric.

Possible Messages

This rule has no predefined messages.

Options

display_name

display_name : str | None = 'Cognitive Complexity'

Description of the metric shown in the dashboard. If None, rfg_metric_name is used.
 

include_disabled_code_sections

include_disabled_code_sections : bool = False

Include code sections that are disabled via preprocessor #if. Does not add routines that where disabled completely. Note: Possibly slow and not 100% accurate due to parsing limitations.
 

include_recursive_call_cycles

include_recursive_call_cycles : bool = True

Whether recursive call cycles should be included in the complexity calculation. If true, 1 plus the number of routines in the cycle will be added to the complexity of a routine if it is part of a recursive call cycle.
 

max_value

max_value : int | None = 20

Maximum allowed value. None if unlimited.
 

min_value

min_value : int | None = 0

Minimum allowed value. None if unlimited.
 

rfg_metric_name

rfg_metric_name : str = 'Metric.CognitiveComplexity'

Name of the node attribute storing the metric value in the RFG.