Is it better to return an empty task or null? WITH#

I have an asynchronous method that will look for jobId for a job scheduling service through Api.

If it does not find results, is it better to return an empty task or null?

As I understand it, when returning a collection, it is better to return an empty collection, rather than null, and using objects, it is better to return zero than an empty object; but with tasks I'm not sure which is better. See Attached Method.

thank

   public virtual Task<int> GetJobRunIdAsync(int jobId)
        {
            var jobMonRequest = new jobmonRequest(true, true, true, true, true, 
            true, true, true, true, true, true, true,
            true,
            true, true, true, DateTime.Today, jobId, null, 0, null, null,
            null, null, 0, 0);

        var jobMonResponseTask = Client.jobmonAsync(jobMonRequest);

        var jobTask = jobMonResponseTask.ContinueWith(task =>
        {
            if (jobMonResponseTask.Result == null )
            {
                var empty = new Task<int>(() => 0); // as i understand creating a task with a predefined result will reduce overhead.

                return empty.Result;   // || is it better to just return null?
            }
            if (jobMonResponseTask.Result.jobrun.Length > 1)
            {
                throw  new Exception("More than one job found, Wizards are abound.");
            }
              return jobMonResponseTask.Result.jobrun.Single().id;
        });

        return jobTask;
    }
+4
source share
4 answers

If it does not find results, is it better to return an empty task or null?

Here are a couple of things to consider:

-, null Task. async null . Task , null " ", , , .

, a Task/Task<T>, , null. null . .

, , .

- . . , , ; be int 0, , int? return null, ? , Task<T> .

, :

:

public virtual async Task<int> GetJobRunIdAsync(int jobId)
{
  var jobMonRequest = ...;
  var jobMonResponse = await Client.jobmonAsync(jobMonRequest);
  if (jobMonResponse == null)
    return 0;
  if (jobMonResponse.jobrun.Length > 1)
    throw  new Exception("More than one job found, Wizards are abound.");
  return jobMonResponse.jobrun.Single().id;
}

, ( ) null:

public virtual async Task<int?> GetJobRunIdAsync(int jobId)
{
  var jobMonRequest = ...;
  var jobMonResponse = await Client.jobmonAsync(jobMonRequest);
  if (jobMonResponse == null)
    return null;
  if (jobMonResponse.jobrun.Length > 1)
    throw  new Exception("More than one job found, Wizards are abound.");
  return jobMonResponse.jobrun.Single().id;
}
+5

. , . int? null jobRun, , .

, , , ?

0

- null, . NullReferenceException.

, null, . Nullable value HasValue Value, :

var jobId = api.GetJobRunIdAsync(1234).Result; //note: highly recommend using async/await here instead of just returning a task
if(jobId.HasValue)
{
   var result = DoSomethingWithId(jobId);
   //continue processing...
}

, , , int.

. , - , - :

var results = await GetResultsForJobId(1234);
if(results != null) {}
// or results?.SomeLinqOperation();

var results = await GetResultsForJobId(1234);
results.SomeLinqOperation();

, , Maybe<T> Optional<T>, Nullable<T>. GitHub https://github.com/nlkl/Optional. :

public struct Optional<T>
{
    private static readonly Optional<T> _readOnlyEmpty = new Optional<T>();
    public static Optional<T> Empty => _readOnlyEmpty;

    public T Value { get; }

    public bool HasValue { get; private set; }

    public Optional(T value)
        : this()
    {
        Value = value;
        HasValue = true;
    }

    public static implicit operator Optional<T>(T value)
    {
        return new Optional<T>(value);
    }

    public static implicit operator T(Optional<T> optional)
    {
        return optional.Value;
    }
}
0

, null, IEnumerable foreach(var item in collection).

Foreach NullReferenceException, null . - .

, , , yield. , , , , .

, null , , , , ( , ..). , , , null , , .

Task. Task . , Task , Task. Task , -?

0
source

Source: https://habr.com/ru/post/1682391/


All Articles