-
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.
[EC93] [C#] Return Task directly (#339)
- Loading branch information
Showing
4 changed files
with
59 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
15 changes: 15 additions & 0 deletions
15
ecocode-rules-specifications/src/main/rules/EC93/EC93.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": "Return Task directly", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
"eco-design", | ||
"performance", | ||
"ecocode" | ||
], | ||
"defaultSeverity": "Minor" | ||
} |
41 changes: 41 additions & 0 deletions
41
ecocode-rules-specifications/src/main/rules/EC93/csharp/EC93.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,41 @@ | ||
:!sectids: | ||
|
||
Return Task directly. | ||
|
||
## Why is this an issue ? | ||
|
||
Asynchronous methods that contain a single awaited statement can be optimized by removing the `async` modifier and returning the awaited `Task` directly. Doing so reduces the overhead of the state machine generated for the asynchronous method, leading to more efficient code and improving the resource efficiency of the application. | ||
|
||
### When can it be ignored ? | ||
|
||
When this rule is applied, exception handling is deferred to the main caller instead, which may or may not be desirable. The decision should be done with consideration to the context in which the method is used. | ||
|
||
## Non-compliant examples | ||
|
||
[source, cs] | ||
---- | ||
public static async Task Test1() | ||
{ | ||
await Task.Delay(1000); // Non-compliant, return the Task directly. | ||
} | ||
public static async Task Test2() | ||
{ | ||
await MyAsyncMethod(); // Non-compliant, exceptions within MyAsyncMethod are handled by the method itself. | ||
} | ||
---- | ||
|
||
## Compliant examples | ||
|
||
[source, cs] | ||
---- | ||
public static Task Test1() | ||
{ | ||
return Task.Delay(1000); // Compliant | ||
} | ||
public static Task Test2() | ||
{ | ||
return MyAsyncMethod(); // Compliant, exceptions within MyAsyncMethod are handled by the caller of Test2. | ||
} | ||
---- |