index = 1
nb = 2
...
if nb == 0:
nb = index
elif nb == 1:
nb = index * 2
elif nb == 2:
nb = index * 3
else:
nb = -1
return nb
Excessive use of nested IF, ELSEIF, or ELSE statements increases cyclomatic complexity and degrades performance. Using a MATCH-CASE statement or refactoring the code reduces CPU overhead and improves readability.
one variable must be used maximum twice in IF / ELSEIF / ELSE statements at the same level - WARNINGs :
IF and ELSEIF statements use explicitly variable names !
ELSE statements use implicity variable names !
one variable must be used maximum twice in IF / ELSEIF / ELSE statements at differents hierarchical levels
we can assume that if one variable is used three times or more, we should :
use a MATCH-CASE statement instead
or refactor the code if possible
Non-compliant, because nb is used 4 times :
- 2 explicit times in IF statements
- 2 implicit times in ELSE statements
index = 1
nb = 2
...
if nb == 0:
nb = index
elif nb == 1:
nb = index * 2
elif nb == 2:
nb = index * 3
else:
nb = -1
return nb
MATCH-CASE statement solution + refactor solution
index = 1
nb = 2
...
match nb:
case 0:
nb = index * (nb + 1)
case 1:
nb = index * (nb + 1)
case 2:
nb = index * (nb + 1)
case _:
nb = -1
return nb