Cognitive Complexity Complexity is a measure of how hard the control flow of a method is to understand.
Methods with high Cognitive Complexity will be difficult to maintain.
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