From d0b2e8cc89a03367d4d845ff40559c3e218708cd Mon Sep 17 00:00:00 2001 From: E000391 Date: Thu, 30 May 2024 11:24:09 +0200 Subject: [PATCH 1/4] add rule EC1369 --- .../src/main/rules/EC1369/EC1369.json | 15 +++++++++++++ .../main/rules/EC1369/java/EC1369.asciidoc | 21 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 ecocode-rules-specifications/src/main/rules/EC1369/EC1369.json create mode 100644 ecocode-rules-specifications/src/main/rules/EC1369/java/EC1369.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules/EC1369/EC1369.json b/ecocode-rules-specifications/src/main/rules/EC1369/EC1369.json new file mode 100644 index 00000000..aea5000b --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC1369/EC1369.json @@ -0,0 +1,15 @@ +{ + "title": "Performance: orElseGet instead of orElse", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "1min" + }, + "tags": [ + "ecocode", + "eco-design", + "performance" + ], + "defaultSeverity": "Minor" +} diff --git a/ecocode-rules-specifications/src/main/rules/EC1369/java/EC1369.asciidoc b/ecocode-rules-specifications/src/main/rules/EC1369/java/EC1369.asciidoc new file mode 100644 index 00000000..77dfc09f --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC1369/java/EC1369.asciidoc @@ -0,0 +1,21 @@ +Parameter of orElse() is evaluated, even when having a non-empty Optional. + +Supplier method of orElseGet passed as an argument is only executed when an Optional value isn’t present. + +Therefore, using orElseGet() will save computing time. + +## Noncompliant Code Example + +```java +Optional.of("ecoCode").orElse(getUnpredictedMethod()); +``` + +## Compliant Code Example + +```java +Optional.of("ecoCode").orElseGet(() -> getUnpredictedMethod()); +``` + +```java +randomClass.orElse(getUnpredictedMethod()); +``` From 032f4d678bf029388f731574e2d98a7977a910f7 Mon Sep 17 00:00:00 2001 From: E000391 Date: Thu, 30 May 2024 15:23:04 +0200 Subject: [PATCH 2/4] update RULES.md --- RULES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/RULES.md b/RULES.md index 05310c4b..6a66a5dd 100644 --- a/RULES.md +++ b/RULES.md @@ -75,6 +75,7 @@ Some are applicable for different technologies. | | 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) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | πŸš€ | | | 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) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚫 | | | 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) | 🚫 | 🚫 | πŸš€ | 🚫 | 🚫 | 🚫 | 🚫 | +| EC1369 | Use orElseGet instead of orElse | Parameter of orElse() is evaluated, even when having a non-empty Optional. Supplier method of orElseGet passed as an argument is only executed when an Optional value isn’t present. Therefore, using orElseGet() will save computing time. | [cnumr best practices (3rd edition) BP_042](https://github.com/cnumr/best-practices/blob/main/chapters/BP_042_fr.md) | 🚫 | 🚫 | πŸš€ | 🚫 | 🚫 | 🚫 | 🚫 | ## Rules to be reworked / measured / clarified From 4d28f85d73256e8335674be31674b5a9c7f62c1c Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Thu, 13 Jun 2024 21:53:46 +0200 Subject: [PATCH 3/4] array format correction of RULES.md --- RULES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RULES.md b/RULES.md index e0ebb5c7..5f3b8b1a 100644 --- a/RULES.md +++ b/RULES.md @@ -66,7 +66,7 @@ Some are applicable for different technologies. | EC87 | Use collection indexer | Collection indexers should be used instead of Linq, when available | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | βœ… | 🚫 | | EC88 | Dispose resource asynchronously | Resources that implement `IAsyncDisposable` should be disposed asynchronously | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | βœ… | 🚫 | | EC89 | Avoid using function cache without limit | If a function has decorators without max size cache, the program will store unlimited datas | | ❓ | ❓ | ❓ | βœ… | ❓ | ❓ | ❓ | -| EC93 | Return `Task` directly | Consider returning a `Task` directly instead of a single `await` | | ❓ | ❓ | ❓ | ❓ | ❓ | βœ… | ❓ | +| EC93 | Return `Task` directly | Consider returning a `Task` directly instead of a single `await` | | ❓ | ❓ | ❓ | ❓ | ❓ | βœ… | ❓ | | EC203 | Detect unoptimized file formats | When it is possible, to use svg format image over other image format | | πŸš€ | πŸš€ | πŸš€ | βœ… | πŸš€ | πŸš€ | 🚫 | | EC404 | Avoid list comprehension in iterations | Use generator comprehension instead of list comprehension in for loop declaration | | 🚫 | 🚫 | 🚫 | βœ… | 🚫 | 🚫 | 🚫 | | | Use official social media sharing buttons | These JavaScript plugins are very resource-intensive: to work, they require a large number of requests and download heavy files. It is better to prefer direct links. | [cnumr best practices (3rd edition) BP_019](https://github.com/cnumr/best-practices/blob/main/chapters/BP_019_fr.md) | 🚫 | 🚫 | πŸš€ | 🚫 | 🚫 | 🚫 | πŸš€ | From 93f94a792552b731e8d1ffc297c94de89c8c29a4 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Thu, 13 Jun 2024 21:59:59 +0200 Subject: [PATCH 4/4] [EC94] update rule referentiel with real rule ID + CHANGELOG.md + RULES.md --- CHANGELOG.md | 1 + RULES.md | 2 +- .../src/main/rules/{EC1369/EC1369.json => EC94/EC94.json} | 0 .../{EC1369/java/EC1369.asciidoc => EC94/java/EC94.asciidoc} | 0 4 files changed, 2 insertions(+), 1 deletion(-) rename ecocode-rules-specifications/src/main/rules/{EC1369/EC1369.json => EC94/EC94.json} (100%) rename ecocode-rules-specifications/src/main/rules/{EC1369/java/EC1369.asciidoc => EC94/java/EC94.asciidoc} (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index a2635e29..1198bd0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - [C# #66](https://github.com/green-code-initiative/ecoCode-csharp/pull/66) [EC93] [C#] Return Task directly +- [JAVA #323](https://github.com/green-code-initiative/ecoCode/pull/323) [EC94] [Java] Use orElseGet instead of orElse ### Changed diff --git a/RULES.md b/RULES.md index 5f3b8b1a..65a8ebc1 100644 --- a/RULES.md +++ b/RULES.md @@ -67,6 +67,7 @@ Some are applicable for different technologies. | EC88 | Dispose resource asynchronously | Resources that implement `IAsyncDisposable` should be disposed asynchronously | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | βœ… | 🚫 | | EC89 | Avoid using function cache without limit | If a function has decorators without max size cache, the program will store unlimited datas | | ❓ | ❓ | ❓ | βœ… | ❓ | ❓ | ❓ | | EC93 | Return `Task` directly | Consider returning a `Task` directly instead of a single `await` | | ❓ | ❓ | ❓ | ❓ | ❓ | βœ… | ❓ | +| EC94 | Use orElseGet instead of orElse | Parameter of orElse() is evaluated, even when having a non-empty Optional. Supplier method of orElseGet passed as an argument is only executed when an Optional value isn’t present. Therefore, using orElseGet() will save computing time. | [cnumr best practices (3rd edition) BP_042](https://github.com/cnumr/best-practices/blob/main/chapters/BP_042_fr.md) | 🚧 | ❓ | ❓ | ❓ | ❓ | ❓ | ❓ | | EC203 | Detect unoptimized file formats | When it is possible, to use svg format image over other image format | | πŸš€ | πŸš€ | πŸš€ | βœ… | πŸš€ | πŸš€ | 🚫 | | EC404 | Avoid list comprehension in iterations | Use generator comprehension instead of list comprehension in for loop declaration | | 🚫 | 🚫 | 🚫 | βœ… | 🚫 | 🚫 | 🚫 | | | Use official social media sharing buttons | These JavaScript plugins are very resource-intensive: to work, they require a large number of requests and download heavy files. It is better to prefer direct links. | [cnumr best practices (3rd edition) BP_019](https://github.com/cnumr/best-practices/blob/main/chapters/BP_019_fr.md) | 🚫 | 🚫 | πŸš€ | 🚫 | 🚫 | 🚫 | πŸš€ | @@ -76,7 +77,6 @@ Some are applicable for different technologies. | | 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) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | πŸš€ | | | 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) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚫 | | | 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) | 🚫 | 🚫 | πŸš€ | 🚫 | 🚫 | 🚫 | 🚫 | -| EC1369 | Use orElseGet instead of orElse | Parameter of orElse() is evaluated, even when having a non-empty Optional. Supplier method of orElseGet passed as an argument is only executed when an Optional value isn’t present. Therefore, using orElseGet() will save computing time. | [cnumr best practices (3rd edition) BP_042](https://github.com/cnumr/best-practices/blob/main/chapters/BP_042_fr.md) | 🚫 | 🚫 | πŸš€ | 🚫 | 🚫 | 🚫 | 🚫 | ## Rules to be reworked / measured / clarified diff --git a/ecocode-rules-specifications/src/main/rules/EC1369/EC1369.json b/ecocode-rules-specifications/src/main/rules/EC94/EC94.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules/EC1369/EC1369.json rename to ecocode-rules-specifications/src/main/rules/EC94/EC94.json diff --git a/ecocode-rules-specifications/src/main/rules/EC1369/java/EC1369.asciidoc b/ecocode-rules-specifications/src/main/rules/EC94/java/EC94.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules/EC1369/java/EC1369.asciidoc rename to ecocode-rules-specifications/src/main/rules/EC94/java/EC94.asciidoc