Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c3ca195
Rule specifications normalization in progress
pbaumard Aug 9, 2024
bd8a4f1
Tests are now OK
pbaumard Sep 4, 2024
706c181
Add tests for title length and prefix
pbaumard Sep 4, 2024
06e0173
Add java version
pbaumard Sep 4, 2024
dabb897
Fix test unsing unsorted list
pbaumard Sep 4, 2024
a0a2c2e
Add license-maven-plugin and spotless-maven-plugin
pbaumard Sep 25, 2024
f23f81d
Revert section change for EC31
pbaumard Sep 25, 2024
613a418
Better section titles in EC36
pbaumard Sep 25, 2024
3975cd0
Revert tag changes
pbaumard Sep 25, 2024
7d53fc2
Consistent descriptions for EC2
pbaumard Sep 25, 2024
0fa8fc7
Move pom changes to ecocode-rules-specifications
pbaumard Sep 25, 2024
a3add9e
Move link to resources
pbaumard Sep 25, 2024
52c0d78
Typography
pbaumard Sep 25, 2024
abfcfea
Better EC69 title
pbaumard Sep 25, 2024
94c42dc
Style
pbaumard Sep 26, 2024
8470f60
Test refactoring
pbaumard Sep 26, 2024
f714c48
Merge remote-tracking branch 'origin/main' into feature/rule-descript…
pbaumard Oct 24, 2024
7b43fe8
Merge remote-tracking branch 'origin/main' into feature/rule-descript…
pbaumard Oct 24, 2024
ffa58e8
Merge branch 'main' into feature/rule-description-normalization
pbaumard Nov 14, 2024
ee7f507
Use standard sustainability tag instead of eco-design
pbaumard Nov 20, 2024
f2f8eff
Merge remote-tracking branch 'origin/main' into feature/rule-descript…
pbaumard Dec 19, 2024
3d2e6c8
Merge remote-tracking branch 'origin/main' into feature/rule-descript…
pbaumard Jan 2, 2025
4973db6
Merge branch 'main' into feature/rule-description-normalization
pbaumard Jan 3, 2025
d6dbc50
Merge branch 'main' into feature/rule-description-normalization
dedece35 Jan 3, 2025
14b4072
Merge branch 'main' into feature/rule-description-normalization
pbaumard Jan 6, 2025
7fc9af0
Fix GCI82 description
pbaumard Jan 6, 2025
3d041ce
Revert pom formatting changes
pbaumard Jan 13, 2025
f5ebd44
Merge branch 'main' into feature/rule-description-normalization
pbaumard Mar 14, 2025
e037fbe
Merge branch 'green-code-initiative:main' into feature/rule-descripti…
pbaumard May 15, 2025
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
377 changes: 190 additions & 187 deletions ecocode-rules-specifications/pom.xml

Large diffs are not rendered by default.

19 changes: 10 additions & 9 deletions ecocode-rules-specifications/src/main/rules/EC1/EC1.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
{
"title": "Avoid Spring repository call in loop or stream operations",
"title": "Spring repository should not be called in loop or stream operations",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you test your modification in a local SonarQube to check if the description is well displayed ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those func attribute changes were reverted.

"func": "Constant/Issue",
"constantCost": "50min"
},
"tags": [
"performance",
"spring",
"eco-design",
"ecocode"
],
"defaultSeverity": "Minor"
"tags": [ "performance", "spring", "eco-design", "ecocode" ],
"defaultSeverity": "Minor",
"code": {
"impacts": {
"RELIABILITY": "LOW"
},
"attribute": "EFFICIENT"
}
}
35 changes: 25 additions & 10 deletions ecocode-rules-specifications/src/main/rules/EC1/java/EC1.asciidoc
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
:!sectids:

== Why is this an issue?

The use of Spring repository in a loop induces unnecessary calculations by the CPU and therefore superfluous energy consumption.
Also, the use of Spring repository in a stream operation like "peek, forEach, forEachOrdered, map" induces unnecessary multiple access to the database instead of single batch call.

## Noncompliant Code Example
== How to fix it
=== Noncompliant code example

```java
[source,java]
----
// Noncompliant
private final List<Integer> ids = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

List<Employee> employees = new ArrayList<>();
Expand All @@ -14,9 +21,11 @@ for (Integer id: ids) {
employees.add(employee.get());
}
}
```
----

```java
[source,java]
----
// Noncompliant
List<Employee> employees = new ArrayList<>();
Stream stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
stream.forEach(id -> {
Expand All @@ -25,16 +34,22 @@ stream.forEach(id -> {
employees.add(employee.get());
}
});
```
----

## Compliant Solution
=== Compliant solution

```java
[source,java]
----
private final List<Integer> ids = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Employee> employees = employeeRepository.findAllById(ids);
```
----

```java
[source,java]
----
private final List<Integer> ids = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).toList();
List<Employee> employees = employeeRepository.findAllById(ids);
```
----

== Resources

Reference/Validation is unknown.
17 changes: 10 additions & 7 deletions ecocode-rules-specifications/src/main/rules/EC10/EC10.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
{
"title": "Avoid using unoptimized vector images",
"title": "Unoptimized vector images should be avoided",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"func": "Constant/Issue",
"constantCost": "60min"
},
"tags": [
"eco-design",
"ecocode"
],
"defaultSeverity": "Minor"
"tags": [ "eco-design", "ecocode", "performance" ],
"defaultSeverity": "Minor",
"code": {
"impacts": {
"RELIABILITY": "LOW"
},
"attribute": "EFFICIENT"
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
:!sectids:

== Why is this an issue?

SVG images generated by common drawing softwares contains unnecessary data: calc layer, metadata, namespaces and comments.

## Noncompliant Code Example
== How to fix it
=== Noncompliant code example

```xml
[source,xml]
----
<!-- Noncompliant -->
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
width="210mm"
Expand All @@ -28,12 +35,17 @@ SVG images generated by common drawing softwares contains unnecessary data: calc
r="73.177132" />
</g>
</svg>
```
----

## Compliant Solution
=== Compliant solution

```xml
[source,xml]
----
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 210 297" width="210mm" height="297mm">
<circle cx="104.02724" cy="152.19028" r="73.177132" style="fill:#ff00ff;stroke-width:0.264583"/>
</svg>
```
----

== Resources

- https://github.com/cnumr/best-practices/blob/main/chapters/BP_036_fr.md[CNumR best practices (3rd edition) BP_036]
27 changes: 10 additions & 17 deletions ecocode-rules-specifications/src/main/rules/EC11/EC11.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
{
"title": "Disallow multiple access of same DOM element.",
"title": "DOM manipulation should be limited",
"type": "CODE_SMELL",
"code": {
"impacts": {
"MAINTAINABILITY": "MEDIUM"
},
"attribute": "EFFICIENT"
},
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"func": "Constant/Issue",
"constantCost": "5min"
},
"tags": [
"ecocode",
"eco-design",
"performance"
],
"tags": [ "ecocode", "eco-design", "performance" ],
"defaultSeverity": "Major",
"compatibleLanguages": [
"JAVASCRIPT",
"TYPESCRIPT"
]
"code": {
"impacts": {
"RELIABILITY": "MEDIUM"
},
"attribute": "EFFICIENT"
},
"compatibleLanguages": [ "JAVASCRIPT", "TYPESCRIPT" ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ Assigning the DOM object to a variable not only improves performance but also en
It makes the code more concise and self-explanatory.
Developers reading the code can understand that the variable holds a reference to a specific DOM element, and its subsequent use is likely for multiple operations.

== How to fix it
=== Noncompliant code example

Here's an example in JavaScript to illustrate this rule:

[source,js,data-diff-id="2",data-diff-type="noncompliant"]
----
const width = document.getElementById('block').clientWidth;
const height = document.getElementById('block').clientHeight; // Non-compliant
const height = document.getElementById('block').clientHeight; // Noncompliant
----

=== Compliant solution

[source,js,data-diff-id="1",data-diff-type="noncompliant"]
----
const blockElement = document.getElementById('block'); // Compliant
Expand All @@ -32,5 +37,5 @@ In the second example, the DOM element reference is cached in the `blockElement`

=== Documentation

- https://github.com/cnumr/best-practices/blob/main/chapters/BP_054_en.md[CNUMR best practices] - Reduce DOM access via JavaScript
- https://github.com/cnumr/best-practices/blob/main/chapters/BP_054_en.md[CNumR best practices] - Reduce DOM access via JavaScript
- https://developer.mozilla.org/en-US/docs/Learn/Performance/JavaScript#tips_for_writing_more_efficient_code[Mozilla Web Technology for Developers] - Tips for writing more efficient code
27 changes: 10 additions & 17 deletions ecocode-rules-specifications/src/main/rules/EC12/EC12.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
{
"title": "Disallow multiple style changes at once.",
"title": "Multiple style changes should be batched",
"type": "CODE_SMELL",
"code": {
"impacts": {
"MAINTAINABILITY": "MEDIUM"
},
"attribute": "EFFICIENT"
},
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"func": "Constant/Issue",
"constantCost": "10min"
},
"tags": [
"ecocode",
"eco-design",
"performance"
],
"tags": [ "ecocode", "eco-design", "performance" ],
"defaultSeverity": "Major",
"compatibleLanguages": [
"JAVASCRIPT",
"TYPESCRIPT"
]
"code": {
"impacts": {
"RELIABILITY": "MEDIUM"
},
"attribute": "EFFICIENT"
},
"compatibleLanguages": [ "JAVASCRIPT", "TYPESCRIPT" ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ Making multiple CSS changes in a single batch can trigger multiple reflows and r
Reflows and repaints are resource-intensive operations that can lead to performance issues.
Applying changes individually minimizes the number of reflows and repaints, improving overall page performance.

== How to fix it
=== Noncompliant code example

Here's an example in JavaScript and CSS to illustrate this rule:

[source,html,data-diff-id="3",data-diff-type="noncompliant"]
----
<script>
element.style.height = "800px";
element.style.width = "600px"; // Non-compliant
element.style.color = "red"; // Non-compliant
element.style.width = "600px"; // Noncompliant
element.style.color = "red"; // Noncompliant
</script>
----

=== Compliant solution

[source,html,data-diff-id="10",data-diff-type="compliant"]
----
<style>
Expand All @@ -42,4 +47,4 @@ In the first example, multiple CSS properties are set in a single batch, while i

=== Documentation

- https://github.com/cnumr/best-practices/blob/main/chapters/BP_045_en.md[CNUMR best practices] - Modify several CSS properties at once
- https://github.com/cnumr/best-practices/blob/main/chapters/BP_045_en.md[CNumR best practices] - Modify several CSS properties at once
17 changes: 5 additions & 12 deletions ecocode-rules-specifications/src/main/rules/EC13/EC13.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
{
"title": "Prefer API collections with pagination.",
"title": "API collections should be preferred with pagination",
"type": "CODE_SMELL",
"code": {
"impacts": {
"MAINTAINABILITY": "MEDIUM"
"RELIABILITY": "MEDIUM"
},
"attribute": "EFFICIENT"
},
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"func": "Constant/Issue",
"constantCost": "30min"
},
"tags": [
"ecocode",
"eco-design",
"nestjs",
"performance"
],
"tags": [ "ecocode", "eco-design", "nestjs", "performance" ],
"defaultSeverity": "Major",
"compatibleLanguages": [
"TYPESCRIPT"
]
"compatibleLanguages": [ "TYPESCRIPT" ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ Pagination ensures that only the relevant data is transferred, conserving bandwi

This rule is built for the https://nestjs.com[NestJS framework] but can work with a controller `@Controller()` and a decorated method `@Get()`.

== How to fix it
=== Noncompliant code example

[source,typescript,data-diff-id="4",data-diff-type="noncompliant"]
----
@Controller()
class Test {
@Get()
public find(): Promise<string[]> {} // Non-compliant
public find(): Promise<string[]> {} // Noncompliant
}
----

=== Compliant solution

[source,typescript,data-diff-id="10",data-diff-type="compliant"]
----
interface Pagination {
Expand All @@ -39,7 +44,7 @@ class Test {

=== Documentation

- https://github.com/cnumr/best-practices/blob/main/chapters/BP_076_en.md[CNUMR best practices] - Avoid transferring large amounts of data for processing tasks
- https://github.com/cnumr/best-practices/blob/main/chapters/BP_076_en.md[CNumR best practices] - Avoid transferring large amounts of data for processing tasks
- https://github.com/ppetzold/nestjs-paginate[nestjs-paginate] - Pagination and filtering helper method using Nest.js framework

=== Articles & blog posts
Expand Down
18 changes: 10 additions & 8 deletions ecocode-rules-specifications/src/main/rules/EC2/EC2.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
{
"title": "Avoid multiple if-else statement",
"title": "Variables should not be used in multiple if...else statements",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"func": "Constant/Issue",
"constantCost": "5min"
},
"tags": [
"eco-design",
"performance",
"ecocode"
],
"defaultSeverity": "Minor"
"tags": [ "eco-design", "performance", "ecocode" ],
"defaultSeverity": "Minor",
"code": {
"impacts": {
"RELIABILITY": "LOW"
},
"attribute": "EFFICIENT"
}
}
Loading