Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions RULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 15 additions & 0 deletions src/main/rules/GCI1200/GCI1200.json
Original file line number Diff line number Diff line change
@@ -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"
}
50 changes: 50 additions & 0 deletions src/main/rules/GCI1200/python/GCI1200.asciidoc
Original file line number Diff line number Diff line change
@@ -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/
Binary file added src/main/rules/GCI1200/python/results.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.