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 84ea2788..65a8ebc1 100644 --- a/RULES.md +++ b/RULES.md @@ -66,7 +66,8 @@ 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` | | ❓ | ❓ | ❓ | ❓ | ❓ | βœ… | ❓ | +| 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) | 🚫 | 🚫 | πŸš€ | 🚫 | 🚫 | 🚫 | πŸš€ | diff --git a/ecocode-rules-specifications/src/main/rules/EC94/EC94.json b/ecocode-rules-specifications/src/main/rules/EC94/EC94.json new file mode 100644 index 00000000..aea5000b --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC94/EC94.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/EC94/java/EC94.asciidoc b/ecocode-rules-specifications/src/main/rules/EC94/java/EC94.asciidoc new file mode 100644 index 00000000..77dfc09f --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC94/java/EC94.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()); +```