diff --git a/CHANGELOG.md b/CHANGELOG.md index 268a4bc9..a2635e29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- [C# #66](https://github.com/green-code-initiative/ecoCode-csharp/pull/66) [EC93] [C#] Return Task directly + ### Changed ### Deleted diff --git a/RULES.md b/RULES.md index 16aa9f99..84ea2788 100644 --- a/RULES.md +++ b/RULES.md @@ -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 datas | | ❓ | ❓ | ❓ | ✅ | ❓ | ❓ | ❓ | +| EC93 | Return `Task` directly | Consider returning a `Task` directly instead of a single `await` | | ❓ | ❓ | ❓ | ❓ | ❓ | ✅ | ❓ | | EC203 | Detect unoptimized file formats | When it is possible, to use svg format image over other image format | | 🚀 | 🚀 | 🚀 | ✅ | 🚀 | 🚀 | 🚫 | | EC404 | Avoid list comprehension in iterations | Use generator comprehension instead of list comprehension in for loop declaration | | 🚫 | 🚫 | 🚫 | ✅ | 🚫 | 🚫 | 🚫 | | | Use official social media sharing buttons | These JavaScript plugins are very resource-intensive: to work, they require a large number of requests and download heavy files. It is better to prefer direct links. | [cnumr best practices (3rd edition) BP_019](https://github.com/cnumr/best-practices/blob/main/chapters/BP_019_fr.md) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | 🚫 | 🚀 | diff --git a/ecocode-rules-specifications/src/main/rules/EC93/EC93.json b/ecocode-rules-specifications/src/main/rules/EC93/EC93.json new file mode 100644 index 00000000..dd3cbc1d --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC93/EC93.json @@ -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" +} diff --git a/ecocode-rules-specifications/src/main/rules/EC93/csharp/EC93.asciidoc b/ecocode-rules-specifications/src/main/rules/EC93/csharp/EC93.asciidoc new file mode 100644 index 00000000..07237306 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC93/csharp/EC93.asciidoc @@ -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. +} +----