Is it wrong to return null in a Task?

0

I have the following implementation of MemoryCache :

public Task<News[]> GetCandidateNewsAsync(string candidate)
{
    return _cache.GetOrCreateAsync(candidate, async factory =>// _cache é um IMemoryCache
    {
        var candidateUri = _candidateUris[candidate];

        if (string.IsNullOrEmpty(candidateUri))
            return null; //retorno nulo aqui (erro)

        var candidateNews = await _candidateNewsClient.GetCandidateNewsAsync(1, candidateUri);

        return candidateNews?.News;
    });
}

I ran a code analyzer on top of my project and it reported the following:

  

Do not return this method, instead return 'Task.FromResult (null)', 'Task.CompletedTask' or 'Task.Delay (0)'

I just did not get it right, what's the problem of returning null on this occasion? What's the point of returning a delay of 0ms as he recommends?

    
asked by anonymous 02.08.2018 / 23:33

2 answers

1

This is a response withdrawal from SOen :

It is not right to return a Task null. A% void% is as if it had never been called, but it was.

Then a Task / Task returned from a method should never be Task<T> . But you can still return the null value within null .

What is the problem of returning null on this occasion?

In my case, it was probably a false alarm from my parser, since I was not returning Task as null, but the null value inside Task , which is totally correct.

By better explaining, the Task % of GetOrCreateAsync is given a generic type ( IMemoryCache ), and a <T> , and as my task was asynchronous, when I returned null it was not returning Func<ICacheEntry, out Task<T>> as null, but the value inside the Task , of type Task to null.

What is the point of returning a delay of 0ms as it recommends?

A% w / w of% is the same as returning% w /%, see how it is implemented in official code of : (line 5407 at the time of writing)

public static Task Delay(int millisecondsDelay, CancellationToken cancellationToken)
{
    // ...
    if (cancellationToken.IsCancellationRequested)
    {
        // return a Task created as already-Canceled
        return Task.FromCanceled(cancellationToken);
    }
    else if (millisecondsDelay == 0)
    {
        // return a Task created as already-RanToCompletion
        return Task.CompletedTask;
    }
    // ...
}
    
07.08.2018 / 19:09
0

From the semantic point of view it does not make sense for a Task to return null. In the asynchronous execution world, you return null is 'as if you had been talking' to the code that a task was not executed or does not exist, however, in your case, it exists.

The error is telling you to get the Task to return a value null ( TaskFromReult(null) ) or else to return that the task was executed ( Task.CompletedTask ).

This is all because this is how tasks are normally used, it is part of good practice. When you perform an asynchronous task, you expect it to return whether it was executed or not, you do not expect it to be null . Think of this kind of analysis as for other people who might come to use your code in the future.

    
02.08.2018 / 23:49