Skip to content

Commit

Permalink
[EC91] [C#] Use Where before OrderBy (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
Djoums authored Jun 17, 2024
1 parent 52885f4 commit 0e1f56b
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- [C# #54](https://github.com/green-code-initiative/ecoCode-csharp/pull/54) [EC92] [C#] Use string.Length instead of comparison with empty string
- [C# #61](https://github.com/green-code-initiative/ecoCode-csharp/issues/61) [EC91] [C#] Use Where before OrderBy

### Changed

Expand Down
1 change: 1 addition & 0 deletions RULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +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 data | ||||||||
| EC91 | Use `Where` before `OrderBy` | Filter elements before sorting them for improved efficiency | ||||||||
| EC92 | Use string.Length instead of comparison with empty string | Comparing a string to an empty string is unnecessary and can be replaced by a call to `string.Length` which is more performant and more readable. | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 || 🚫 |
| 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) | 🚧 |||||||
Expand Down
16 changes: 16 additions & 0 deletions ecocode-rules-specifications/src/main/rules/EC91/EC91.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"title": "Use Where before OrderBy",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"eco-design",
"ecocode",
"performance",
"bad-practice"
],
"defaultSeverity": "Major"
}
101 changes: 101 additions & 0 deletions ecocode-rules-specifications/src/main/rules/EC91/csharp/EC91.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
:!sectids:

Use Where before OrderBy.

## Why is this an issue ?

When `Orderby` is called right before `Where`, LINQ sorts all the elements before filtering them. Doing the opposite is more efficient, first filter the items then sort the remaining ones only.

### When can it be ignored ?

This rule shouldn't be ignored.

## Non-compliant examples

[source, cs]
----
public static async Task Test()
{
var query = items
.OrderBy(x => x)
.Where(x => x > 10);
}
----

[source, cs]
----
public static async Task Test()
{
var query = items
.OrderBy(x => x)
.ThenByDescending(x => x)
.Where(x => x > 10);
}
----

[source, cs]
----
public static async Task Test()
{
var query = from item in items
orderby item
where item > 10
select item;
}
----

[source, cs]
----
public static async Task Test()
{
var query = from item in items
orderby item descending
where item > 10
select item;
}
----

## Compliant examples

[source, cs]
----
public static async Task Test()
{
var query = items
.Where(x => x > 10)
.OrderBy(x => x);
}
----

[source, cs]
----
public static async Task Test()
{
var query = items
.Where(x => x > 10)
.OrderBy(x => x)
.ThenByDescending(x => x);
}
----

[source, cs]
----
public static async Task Test()
{
var query = from item in items
where item > 10
orderby item
select item;
}
----

[source, cs]
----
public static async Task Test()
{
var query = from item in items
where item > 10
orderby item descending
select item;
}
----

0 comments on commit 0e1f56b

Please sign in to comment.