Skip to main content
Changed title to state the intent of the code instead of the authors concerns about it
Source Link

Consider we have three different database stores which we want to find an Id which is unique in all this three. I mean if we query first data store and we find that id. It's over and there is no need to further attempt, if that Id is in non of this three data stores we return null, I'm new in asyncasynchronous programming, I wanted to know if there is any better implementation for this lines of code? I mean less if/else?

 public async Task<WebData> GetDataByIdAsync(GetDataById input)
    {
        var twitterData = await DataAccess.FindDataById<TwitterData>(input.Id);
        WebData result;
        if (twitterData  == null)
        {
            var facebookData = await DataAccess.FindDataById<FacebookData>(input.Id);
            if facebookData == null)
            {
                var linkedinData = await DataAccess.FindDataById<LinkedinData>(input.Id);
                if linkedinData == null)
                {
                    return null;
                }
                result = linkedinData 
            }
            else
            {
                result = facebookData;
            }
        }
        else
        {
            result = twitterData ;
        }
        return result;
    }

Consider we have three different database stores which we want to find an Id which is unique in all this three. I mean if we query first data store and we find that id. It's over and there is no need to further attempt, if that Id is in non of this three data stores we return null, I'm new in async programming, I wanted to know if there is any better implementation for this lines of code? I mean less if/else?

 public async Task<WebData> GetDataByIdAsync(GetDataById input)
    {
        var twitterData = await DataAccess.FindDataById<TwitterData>(input.Id);
        WebData result;
        if (twitterData  == null)
        {
            var facebookData = await DataAccess.FindDataById<FacebookData>(input.Id);
            if facebookData == null)
            {
                var linkedinData = await DataAccess.FindDataById<LinkedinData>(input.Id);
                if linkedinData == null)
                {
                    return null;
                }
                result = linkedinData 
            }
            else
            {
                result = facebookData;
            }
        }
        else
        {
            result = twitterData ;
        }
        return result;
    }

Consider we have three different database stores which we want to find an Id which is unique in all this three. I mean if we query first data store and we find that id. It's over and there is no need to further attempt, if that Id is in non of this three data stores we return null, I'm new in asynchronous programming, I wanted to know if there is any better implementation for this lines of code? I mean less if/else?

 public async Task<WebData> GetDataByIdAsync(GetDataById input)
    {
        var twitterData = await DataAccess.FindDataById<TwitterData>(input.Id);
        WebData result;
        if (twitterData  == null)
        {
            var facebookData = await DataAccess.FindDataById<FacebookData>(input.Id);
            if facebookData == null)
            {
                var linkedinData = await DataAccess.FindDataById<LinkedinData>(input.Id);
                if linkedinData == null)
                {
                    return null;
                }
                result = linkedinData 
            }
            else
            {
                result = facebookData;
            }
        }
        else
        {
            result = twitterData ;
        }
        return result;
    }
Changed title to state the intent of the code instead of the authors concerns about it
Link

Is there any less if/else implementation in this async Async method with three internal tasks?

Tweeted twitter.com/StackCodeReview/status/949079355937951744
Source Link
someone
  • 207
  • 1
  • 5

Is there any less if/else implementation in this async method with three internal tasks?

Consider we have three different database stores which we want to find an Id which is unique in all this three. I mean if we query first data store and we find that id. It's over and there is no need to further attempt, if that Id is in non of this three data stores we return null, I'm new in async programming, I wanted to know if there is any better implementation for this lines of code? I mean less if/else?

 public async Task<WebData> GetDataByIdAsync(GetDataById input)
    {
        var twitterData = await DataAccess.FindDataById<TwitterData>(input.Id);
        WebData result;
        if (twitterData  == null)
        {
            var facebookData = await DataAccess.FindDataById<FacebookData>(input.Id);
            if facebookData == null)
            {
                var linkedinData = await DataAccess.FindDataById<LinkedinData>(input.Id);
                if linkedinData == null)
                {
                    return null;
                }
                result = linkedinData 
            }
            else
            {
                result = facebookData;
            }
        }
        else
        {
            result = twitterData ;
        }
        return result;
    }