diff --git a/CHANGELOG.md b/CHANGELOG.md index d153ef88..987a4da6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#400](https://github.com/green-code-initiative/creedengo-rules-specifications/pull/400) Add rule GCI535 - Prefer usage of Intl.NumberFormat +### Added +- [#407](https://github.com/green-code-initiative/creedengo-rules-specifications/pull/407): Detect a for loop and suggest a list comprehension + ### Changed ### Deleted diff --git a/RULES.md b/RULES.md index d4916861..0c4e4684 100644 --- a/RULES.md +++ b/RULES.md @@ -87,6 +87,7 @@ Some are applicable for different technologies. | GCI535 | Use native Intl.NumberFormat to format numbers | There's no need to use a library to display formatted numbers in a recent browser. Use Intl.NumberFormat for that use case. | | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚫 | +| GCI1200 | To use list comprehension instead for loop in simple iterations | | | ❓ | ❓ | πŸš€ | ❓ | ❓ |❓ |❓ | ## Rules to be reworked / measured / clarified diff --git a/src/main/rules/GCI1200/GCI1200.json b/src/main/rules/GCI1200/GCI1200.json new file mode 100644 index 00000000..22fc9b2a --- /dev/null +++ b/src/main/rules/GCI1200/GCI1200.json @@ -0,0 +1,15 @@ +{ + "title": "Use list comprehension instead for loop in simple iterations", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "creedengo", + "eco-design", + "performance" + ], + "defaultSeverity": "Minor" + } \ No newline at end of file diff --git a/src/main/rules/GCI1200/python/GCI1200.asciidoc b/src/main/rules/GCI1200/python/GCI1200.asciidoc new file mode 100644 index 00000000..594c040a --- /dev/null +++ b/src/main/rules/GCI1200/python/GCI1200.asciidoc @@ -0,0 +1,50 @@ +When you use a for loop, on every iteration, you have to look up the variable holding +the list and then call its append() function. A list comprehension can be faster +than a for loop because it’s optimized for performance by Python’s internal mechanisms. + +== Non Compliant Code Example +[source,python] +---- +result = [] +for i in range(0,20): + result.append(i) +---- + +== Compliant Solution +[source,python] +---- +result = [i for i in range(0,20)] +---- + +== Relevance Analysis + +This rule is relevant for scenarios where is needed a simple iteration. +The list comprehensions are often not only more readable +but also faster than using "for loops." +They can simplify the code, but if you put too much logic inside, +they will instead become harder to read and understand. + +=== Configuration + +* Processor: Apple M1 +* RAM: 16 Go +* CO2 Emissions Measurement: Using https://mlco2.github.io/codecarbon/[CodeCarbon] + +=== Context + +Two approaches were benchmarked: +- *Non-compliant:* For loop +- *Compliant:* List comprehension + +=== Impact Analysis + +image::results.png[] + +== Conclusion + +The result of the tests determinated that the list comprehensions +can be more readable, but also an alternative to reduce the C02 emissions. + +== References + +- https://realpython.com/list-comprehension-python/ diff --git a/src/main/rules/GCI1200/python/results.png b/src/main/rules/GCI1200/python/results.png new file mode 100644 index 00000000..55b463dd Binary files /dev/null and b/src/main/rules/GCI1200/python/results.png differ