This rule raises an issue when the code cognitive complexity of a function is above a certain threshold.
Cognitive Complexity is a measure of how hard it is to understand the control flow of a unit of code. Code with high cognitive complexity is hard to read, understand, test, and modify.
As a rule of thumb, high cognitive complexity is a sign that the code should be refactored into smaller, easier-to-manage pieces.
Here are the core concepts:
The method of computation is fully detailed in the pdf linked in the resources.
Developers spend more time reading and understanding code than writing it. High cognitive complexity slows down changes and increases the cost of maintenance.
Function Abs(ByVal n As Integer) As Integer ' Noncompliant: cognitive complexity = 5
If n >= 0 Then ' +1
Return n
Else ' +2, due to nesting
If n = Integer.MinValue Then ' +1
Throw New ArgumentException("The absolute value of int.MinValue is outside of int boundaries")
Else ' +1
Return -n
End If
End If
End Function
They should be refactored to have lower complexity:
Function Abs(ByVal n As Integer) As Integer ' Compliant: cognitive complexity = 3
If n = Integer.MinValue Then ' +1
Throw New ArgumentException("The absolute value of int.MinValue is outside of int boundaries")
Else If n >= 0 Then ' +1
Return n
Else ' +1
Return -n
End If
End Function