Search code examples
linqc#-4.0splitnull-check

What is the most efficient way or best practice for null check when you use Split and FirstOrDefault methods together?


I use Split and LastOrDefault methods together, I use this code block for null check. But it does not seems the most efficient way to me. Because I use lots of check and && operators. It seem a little ugly. Is there any way to accomplish this null-check in a better way? (I searched on web but couldn' t find related answers.)

Note : C# Language version 4.0

Here is my code :

    if (HttpContext.Current.Request.Url.AbsolutePath.Split('/') != null &&
        HttpContext.Current.Request.Url.AbsolutePath.Split('/').Length > 0 &&
        HttpContext.Current.Request.Url.AbsolutePath.Split('/').Last() != null &&
        HttpContext.Current.Request.Url.AbsolutePath.Split('/').Last().Split('.') != null &&
        HttpContext.Current.Request.Url.AbsolutePath.Split('/').Last().Split('.').Length > 0 &&
        HttpContext.Current.Request.Url.AbsolutePath.Split('/').Last().Split('.').First() != null)
    {
        pageName = HttpContext.Current.Request.Url.AbsolutePath.Split('/').LastOrDefault().Split('.').FirstOrDefault();
    }

Thanks for all answers.


Solution

  • The tests are all not needed:

    First, don't run Split multiple times on the same data:

    var splitSlashAbsPath = HttpContext.Current.Request.Url.AbsolutePath.Split('/');
    

    The return array from Split can never be null

    //    if (splitSlashAbsPath != null &&
    

    the return array from Split can never be zero length

    //        splitSlashAbsPath.Length > 0 &&
    

    so the return from Last() can never be null

    //        splitSlashAbsPath.Last() != null &&
    

    Don't run split multiple times on the same data (and calling Last on an array doesn't make sense)

    var splitDotAbsPath = splitSlashAbsPath[splitSlashAbsPath.Length-1].Split('.');
    

    the return array from Split can never be null

    //        splitDotAbsPath != null &&
    

    the return array from Split can never be zero length

    //        splitDotAbsPath.Length > 0 &&
    

    so, the First() from Split can never be null

    //        splitDotAbsPath.First() != null)
    //    {
    

    since you can call Last, calling LastOrDefault makes no sense same for FirstOrDefault

    //        pageName = splitDotAbsPath.FirstOrDefault();
    

    Calling First on an array also doesn't make sense

    pageName = splitDotAbsPath[0];
    
    //    }
    

    So, in summary you have:

    var splitSlashAbsPath = HttpContext.Current.Request.Url.AbsolutePath.Split('/');
    var splitDotAbsPath = splitSlashAbsPath[splitSlashAbsPath.Length-1].Split('.');
    pageName = splitDotAbsPath[0];
    

    However, in general, using Split for just getting one element is very inefficient, so this would be better:

    var path = HttpContext.Current.Request.Url.AbsolutePath;
    var pastSlashPos = path.LastIndexOf('/') + 1;
    var countUntilDot = path.IndexOf('.', pastSlashPos);
    countUntilDot = (countUntilDot >= 0 ? countUntilDot : path.Length) - pastSlashPos;
    pageName = path.Substring(pastSlashPos, countUntilDot);