1

I have some element event handler uses if statement inside, why sonar complains this if statement causes cognitive complexity? Is there a way I can avoid this or improve this?

const handleOnBlur = () => {
    if (isEditMode) {
        setMyStuff(false);
    }
};

This is counted as code smell +2, including 1 for nesting. Why if statement causes code smell especially I don't have very complex if, else or nested if statements.

Thank you!

spspli
  • 3,128
  • 11
  • 48
  • 75
  • Such complexity is a necessary result of (any useful) logic an app implements. You can't eliminate it, but in some cases, reducing it when possible helps. You can think of it a bit like LoC. Aim isn't to reduce it to 0, but to make an attempt to keep things DRY so it isn't a lot larger than it needs to be. – CertainPerformance Oct 30 '21 at 02:54
  • Can you post the entire sonarcube message, please? – Bergi Oct 30 '21 at 03:40
  • In sonarcube it just says brain-overload, and for the if above it increase the complexity by +2 and says "include 1 for nesting" – spspli Oct 31 '21 at 19:51
  • I am in react, and even for component conditional generation, I get +1 complexity. My code is like this: {showMyInputInViewMode && ( )} {!showMyInputTextInViewMode && (
    ...
    )}
    – spspli Oct 31 '21 at 19:53
  • for each && above the code complexity +1 – spspli Oct 31 '21 at 19:57

0 Answers0