David S.
David S.

Reputation: 6105

Naming convention for async functions with async delegates

I am developing an API and I am looking for a good, understandable naming convention.

Consider these different type of overloads of the same functionality:

public async Task<TResult> DoAsync<T,TResult>(Func<T,TResult> func)
{
   var t = await ProcessAsync();
   return func(t);
}

public async Task<TResult> DoAsyncAlsoAwaitDelegateAsync<T,TResult>(Func<T,Task<TResult>> funcAsync);
{
   var t = await ProcessAsync();
   return await funcAsync(t);
}

Note that I cannot make an overload DoAsync<T,TResult>(Func<T,Task<TResult>> func) because it would result in ambiguous calls.

Users could solve the ambiguous call by specifying the parameter name when calling, but I would rather not create unnecessary confusion if possible.

DoAlsoAwaitDelegateAsync is a pretty clumsy name, so I'm looking for alternative naming conventions here.

Upvotes: 0

Views: 762

Answers (1)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239646

If you have to accept a delegate at all (rather than just returning the computed value to the caller and letting them do their own function/task composition) I would just have the version that accepts a Func<T,Task<TResult>>.

Leave it up to the caller whether they have their own asynchronous function or just give you a function that executes entirely synchronously and then returns Task.FromResult(). Rather than (currently) writing two versions of your function that just differs in whether it knows that the delegate may exploit some asynchrony.

Upvotes: 2

Related Questions