Skip to content

Commit

Permalink
add rule EC1369
Browse files Browse the repository at this point in the history
  • Loading branch information
E000391 committed May 30, 2024
1 parent df4c4e2 commit d0b2e8c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
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 d0b2e8c

Please sign in to comment.