-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #323 from Samdespion/main
[Challenge 2024 - HAFNIUM] EC1369 Use orElseGet instead of orElse
- Loading branch information
Showing
3 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
ecocode-rules-specifications/src/main/rules/EC1369/EC1369.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} |
21 changes: 21 additions & 0 deletions
21
ecocode-rules-specifications/src/main/rules/EC1369/java/EC1369.asciidoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
``` |