Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface naming convention for method returning Task [closed]

Consider the following interface and implementations.

interface IService
{
    Task<string> GetAnswer(string question);
}

class SomeService : IService
{
    async Task<string> IService.GetAnswer(string question)
    {
        ... code using awaits ...
    }
}

class AnotherService : IService
{
    Task<string> IService.GetAnswer(string question)
    {
        return Task.FromResult("I have no idea.");
    }
}

According to the Microsoft naming conventions, should the interface method be named GetAnswer or GetAnswerAsync?

By convention, you append "Async" to the names of methods that have an Async or async modifier.

The problem is that the first implementation uses the async modifier, indicating it should receive the "Async" method name suffix, but the second implementation does not use the async modifier, indicating it should not receive the "Async" method name suffix. The two method names in the implementations are forced to be the same by the interface, so I am forced to violate the naming conventions for one of the two classes.

Note I'm not looking for opinionated answers. Consider it multiple choice. :)

  1. You should use the "Async" suffix because the naming conventions say so. (With reference.)
  2. You should not use the "Async" suffix because the naming conventions say so. (With reference.)
  3. The naming conventions don't say. (This will need to come from someone well-versed in them.)
like image 631
Timothy Shields Avatar asked Jul 15 '14 19:07

Timothy Shields


People also ask

Is an async method that returns task?

Async methods can have the following return types: Task, for an async method that performs an operation but returns no value. Task<TResult>, for an async method that returns a value. void , for an event handler.

Should async methods be named async?

By convention, you append "Async" to the names of methods that have an async modifier. You can ignore the convention where an event, base class, or interface contract suggests a different name. For example, you shouldn't rename common event handlers, such as Button1_Click .

What is task and async?

Async code uses Task<T> and Task , which are constructs used to model work being done in the background. The async keyword turns a method into an async method, which allows you to use the await keyword in its body.

What is task in async and await?

Async and await in C# are the code markers, which marks code positions from where the control should resume after a task completes. Let's start with practical examples for understanding the programming concept.


1 Answers

You should use XAsync even when there isn't an async modifier as long as the method represents a complete task based asynchronous operation.

To be technical about it, the passage you quoted tells you to add an Async when there is an async modifier but doesn't tell what to do when there isn't any.

The async modifier is really not a part of the method's signature and you could easily accomplish the exact same behavior without it. If you look at the Task-based Asynchronous Pattern you wouldn't find a reference to the specific async modifier, but to the more broad definition of an async method.

In the .Net framework itself, you can't even know which Async method actually uses the async modifier. A lot (if not most) return TaskCompletionSource.Task to allow you (as a user) to use async-await. For example, this is Stream.WriteAsync:

public virtual Task WriteAsync(Byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
    // If cancellation was requested, bail early with an already completed task.
    // Otherwise, return a task that represents the Begin/End methods.
    return cancellationToken.IsCancellationRequested
                ? Task.FromCancellation(cancellationToken)
                : BeginEndWriteAsync(buffer, offset, count);
}
like image 169
i3arnon Avatar answered Oct 25 '22 19:10

i3arnon