sourcery_analytics.metrics.cognitive_complexity¶
Cognitive complexity calculations.
Cognitive complexity is intended to reflect the difficulty in understanding deeply-nested code, especially nested conditionals.
- sourcery_analytics.metrics.cognitive_complexity.method_cognitive_complexity(method: FunctionDef) int ¶
Calculates the total cognitive complexity of the method body.
- Parameters
method – a node for a function definition
Examples
>>> source = ''' ... def check_add(x, y): ... if x: ... if y: ... return x + y ... ''' >>> method_cognitive_complexity(source) 3
- sourcery_analytics.metrics.cognitive_complexity.total_cognitive_complexity(node: NodeNG)¶
Calculates the total cognitive complexity of all sub-nodes within the node.
Examples
>>> total_cognitive_complexity('''x + y if x and y else 0''') 2
- class sourcery_analytics.metrics.cognitive_complexity.CognitiveComplexityVisitor(_nesting=0)¶
-
Visitor to calculate the cognitive complexity of a node.
Cognitive complexity is contextual. Alone, its value is 1 for control flow elements. It is incremented by 1 for each level of “nesting” within control flow structures.