From 1c117bc2aaf8c5197fe2cd549a4bf87456f0c4a5 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Sat, 5 Aug 2023 23:47:34 +0200 Subject: [PATCH 1/2] [ISSUE 142] new python rule : AvoidMultipleIfElseStatement --- .../src/main/rules/EC2/php/EC2.asciidoc | 1 + .../src/main/rules/EC2/python/EC2.asciidoc | 54 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 ecocode-rules-specifications/src/main/rules/EC2/python/EC2.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules/EC2/php/EC2.asciidoc b/ecocode-rules-specifications/src/main/rules/EC2/php/EC2.asciidoc index 2e77a982a..eae2c2817 100644 --- a/ecocode-rules-specifications/src/main/rules/EC2/php/EC2.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC2/php/EC2.asciidoc @@ -31,6 +31,7 @@ if ($nb == 0) { } else { $nb = -1; } +return $nb; ``` ## Compliant Code Example diff --git a/ecocode-rules-specifications/src/main/rules/EC2/python/EC2.asciidoc b/ecocode-rules-specifications/src/main/rules/EC2/python/EC2.asciidoc new file mode 100644 index 000000000..f9de6b834 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC2/python/EC2.asciidoc @@ -0,0 +1,54 @@ +If we are using too many conditional IF, ELSEIF or ELSE statements it will impact performance. +We can think of using a switch statement instead of multiple if-else if possible, or refactor code +to reduce number of IF, ELSEIF and ELSE statements. Sometimes called "complexity cyclomatic". +MATCH-CASE statement has a performance advantage over if – else. + +## Functional rules +- 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 MATCHS-CASE statement instead +- or refactor the code if possible + +## Non-compliant Code Example + +NON compliant, because `nb` is used 4 times : +- 2 explicit times in IF statements +- 2 implicit times in ELSE statements + +```python +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 +``` + +## Compliant Code Example + +MATCH-CASE statement solution + refactor solution + +```python +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 +``` From a5501c12ab4ab698bae5a0ea3ead8b8399ad418e Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Mon, 7 Aug 2023 10:43:16 +0200 Subject: [PATCH 2/2] [ISSUE 142] typo --- .../src/main/rules/EC2/python/EC2.asciidoc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ecocode-rules-specifications/src/main/rules/EC2/python/EC2.asciidoc b/ecocode-rules-specifications/src/main/rules/EC2/python/EC2.asciidoc index f9de6b834..e2688d834 100644 --- a/ecocode-rules-specifications/src/main/rules/EC2/python/EC2.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC2/python/EC2.asciidoc @@ -9,12 +9,12 @@ MATCH-CASE statement has a performance advantage over if – else. - 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 MATCHS-CASE statement instead +- use a MATCH-CASE statement instead - or refactor the code if possible ## Non-compliant Code Example -NON compliant, because `nb` is used 4 times : +Non-compliant, because `nb` is used 4 times : - 2 explicit times in IF statements - 2 implicit times in ELSE statements