SonarJS icon indicating copy to clipboard operation
SonarJS copied to clipboard

S3776 should exclude the complexity of JSX attribute short-circuits and nested function definitions

Open gabriel-vivas-sonarsource opened this issue 2 years ago • 1 comments

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:

  1. 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.
  2. 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

  1. We should probably ignore simple booleans in JSX attributes when counting the Cognitive Complexity of a component
  2. 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

saberduck avatar Aug 09 '22 07:08 saberduck

See some of the comments in https://github.com/SonarSource/SonarJS/issues/2238 As people keep coming to this subject.