-
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.
[EC91] [C#] Use Where before OrderBy (#341)
- Loading branch information
Showing
4 changed files
with
119 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
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
16 changes: 16 additions & 0 deletions
16
ecocode-rules-specifications/src/main/rules/EC91/EC91.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,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
101
ecocode-rules-specifications/src/main/rules/EC91/csharp/EC91.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,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; | ||
} | ||
---- |