Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EC84] Avoid async void method #27

Closed
Djoums opened this issue Apr 14, 2024 · 0 comments · Fixed by #29
Closed

[EC84] Avoid async void method #27

Djoums opened this issue Apr 14, 2024 · 0 comments · Fixed by #29
Assignees
Labels
🗃️ rule rule improvment or rule development or bug 🚀 enhancement New feature or request

Comments

@Djoums
Copy link
Collaborator

Djoums commented Apr 14, 2024

Category : Usage

Severity : Warning

Why is this an issue ?

Using async void methods is strongly discouraged due to the significant drawbacks that affect both performance and environmental impact:

  • They differ fundamentally from async Task methods as they cannot be awaited and do not return a task. This complicates error handling and prevents their use in method chains, making them more difficult to test, debug and optimize.
  • Because they obscure exceptions, they can lead to unhandled errors that can remain undetected. This can cause silent performance degradation and increase the application's carbon footprint by necessitating additional computing resources to manage avoidable failures.
  • The lack of robust error handling also directly impacts the application stability, leading to potential lags and crashes. These issues not only degrade user experience but also contribute to unnecessary energy consumption as systems work harder to recover from failures.

Overall, async void methods are considered poor practice, especially since the recommended alternative async Task is readily usable and provides an efficient solution for managing asynchronous operations without these downsides.

When can it be ignored ?

In some edge cases, using an async void method can be beneficial, but you should really know what you're doing.

Examples

public class Test
{
    public async void Run()
    {
        WithAsyncVoid(); // Cannot be awaited
        await WithAsyncTask();
    }

    public async void WithAsyncVoid() // Non-compliant, use async Task
    {
        await Task.Delay(1000);
        Console.WriteLine();
    }

    public async Task WithAsyncTask() // Compliant
    {
        await Task.Delay(1000);
        Console.WriteLine();
    }
}
@Djoums Djoums self-assigned this Apr 15, 2024
@Djoums Djoums added 🗃️ rule rule improvment or rule development or bug 🚀 enhancement New feature or request labels Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🗃️ rule rule improvment or rule development or bug 🚀 enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant