Skip to content

Commit

Permalink
Merge pull request #323 from Samdespion/main
Browse files Browse the repository at this point in the history
[Challenge 2024 - HAFNIUM] EC1369 Use orElseGet instead of orElse
  • Loading branch information
dedece35 authored Jun 13, 2024
2 parents 98aac72 + f71d21a commit b6c2ea9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions RULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,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

Expand Down
15 changes: 15 additions & 0 deletions ecocode-rules-specifications/src/main/rules/EC1369/EC1369.json
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"
}
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());
```

0 comments on commit b6c2ea9

Please sign in to comment.