Skip to content

Commit 4ca4e03

Browse files
feat: add rule for loop vs list comprehension
1 parent 458ab17 commit 4ca4e03

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- Added rule GCI1200: Detect a for loop and suggest a list comprehension
13+
1214
### Changed
1315

1416
- Correction of various typos in rules documentations

RULES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Some are applicable for different technologies.
8383
| | Resize images browser-side | Do not resize images using the HEIGHT and WIDTH attributes of the HTML code. This approach requires transferring these images to their original size, wasting bandwidth and CPU cycles. | [cnumr best practices (3rd edition) BP_034](https://github.com/cnumr/best-practices/blob/main/chapters/BP_034_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚀 |
8484
| | Modify the DOM when traversing it | Modifying the DOM (Document Object Model) as you traverse it can lead to situations where the loop becomes very resource-intensive, especially CPU cycles. | [cnumr best practices (3rd edition) BP_041](https://github.com/cnumr/best-practices/blob/main/chapters/BP_041_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚫 |
8585
| | Edit DOM elements to make it invisible | When an element of the Document Object Model (DOM) needs to be modified by several properties, each change in style or content will generate a repaint or reflow. | [cnumr best practices (3rd edition) BP_042](https://github.com/cnumr/best-practices/blob/main/chapters/BP_042_fr.md) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | 🚫 | 🚫 |
86+
| GSI1200 | To use list comprehension instead for loop in simple iterations | | ||| 🚀 |||||
8687

8788
## Rules to be reworked / measured / clarified
8889

src/main/rules/GCI1200/GCI1200.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"title": "To use list comprehension instead for loop in simple iterations",
3+
"type": "CODE_SMELL",
4+
"status": "ready",
5+
"remediation": {
6+
"func": "Constant\/Issue",
7+
"constantCost": "5min"
8+
},
9+
"tags": [
10+
"creedengo",
11+
"eco-design",
12+
"performance",
13+
"comprehension",
14+
"loop",
15+
"iteration"
16+
],
17+
"defaultSeverity": "Minor"
18+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
When you use a for loop, on every iteration, you have to look up the variable holding
2+
the list and then call its append() function. A list comprehension can be faster
3+
than a for loop because it’s optimized for performance by Python’s internal mechanisms.
4+
5+
== Non Compliant Code Example
6+
[source,python]
7+
----
8+
result = []
9+
for i in range(0,20):
10+
result.append(i)
11+
----
12+
13+
== Compliant Solution
14+
[source,python]
15+
----
16+
result = [i for i in range(0,20)]
17+
----
18+
19+
== Relevance Analysis
20+
21+
This rule is relevant for scenarios where is needed a simple iteration.
22+
The list comprehensions are often not only more readable
23+
but also faster than using "for loops."
24+
They can simplify the code, but if you put too much logic inside,
25+
they will instead become harder to read and understand.
26+
27+
=== Configuration
28+
29+
* Processor: Apple M1
30+
* RAM: 16 Go
31+
* CO2 Emissions Measurement: Using https://mlco2.github.io/codecarbon/[CodeCarbon]
32+
33+
=== Context
34+
35+
Two approaches were benchmarked:
36+
- *Non-compliant:* For loop
37+
- *Compliant:* List comprehension
38+
39+
=== Impact Analysis
40+
41+
image::results.png[]
42+
43+
== Conclusion
44+
45+
The result of the tests determinated that the list comprehensions
46+
can be more readable, but also an alternative to reduce the C02 emissions.
47+
48+
== References
49+
50+
- https://realpython.com/list-comprehension-python/
306 KB
Loading

0 commit comments

Comments
 (0)