SonarJS
SonarJS copied to clipboard
S3776 should exclude the complexity of JSX attribute short-circuits and nested function definitions
Label | Description |
---|---|
Rule | S3776 "Cognitive Complexity of functions should not be too high" |
FP Example | * Booleans inside JSX * Server definition |
Explanation
Two different improvements for two different cases:
- In JSX, it is fairly common to use short-circuit logic for conditionally rendering attribute values. Currently, we count this as Cognitive Complexity. See the example.
- Creating functions inside a closure is a very common pattern in JavaScript. For example when creating event handlers for a new connection on a WebSocket server in NodeJS. In these cases, we are currently reporting a complexity measure that aggregates the "child" function's complexity. This means the parent functions appear to have very high complexity when in reality it is doing little.
Suggestion
- We should probably ignore simple booleans in JSX attributes when counting the Cognitive Complexity of a component
- We should probably ignore the added complexity of functions defined in the scope of the parent function. Each function should get its own, independent Cognitive Complex score.
we didn't manage to do this in current sprint, removing from milestone
See some of the comments in https://github.com/SonarSource/SonarJS/issues/2238 As people keep coming to this subject.