Should I return true or false if both of the IP address strings are empty?

Posted on

Problem

I am validating two IP addresses. I simply wrote a peice of code like

if(string.IsNullOrEmpty(ip1) && string.IsNullOrEmpty(ip2) 
  return true;

But is this logically correct? What should we done with empty strings? If both are empty strings, is it okay to return true?

May be a complete code snipet can give better insight.

public static bool IsIPAddressMatching(string ipAddressA, string ipAddressB)
        {
            try
            {
                if(string.IsNullOrEmpty(ipAddressA) || string.IsNullOrEmpty(ipAddressB))
                    return true;
                var address1 = IPAddress.Parse(ipAddressA);
                var address2 = IPAddress.Parse(ipAddressB);

                if (address1.AddressFamily == address2.AddressFamily)
                    return (address1.Equals(address2));

               // some code here to check mapped IP addresses

            }
            catch (Exception)
            {
                return false;
            }
        }

Solution

From the IP validation point of view null or empty IP addresses are exceptional actions so you can not say that two null or empty strings are the same IP addresses becouse they aren’t IP addresses and sometimes not even strings.

And you don’t have to sorrund the logic with try-catch block becouse it will eat usefull information when things will get ugly. If you don’t want to deal with parsing exception then use IPAddress.TryParse it will be faster becouse throwing an exception is always a heavy weight stuff (check it with profiler).

public static bool IsIPAddressMatching(string ipAddressA, string ipAddressB)
{
    if (ipAddressA == null)
    {
        throw new ArgumentNullException("ipAddressA");
    }
    if (ipAddressB == null)
    {
        throw new ArgumentNullException("ipAddressB");
    }

    if (string.IsNullOrEmpty(ipAddressA))
    {
        throw new ArgumentException("ipAddressA cannot be an empty string", "ipAddressA");
    }
    if (string.IsNullOrEmpty(ipAddressB))
    {
        throw new ArgumentException("ipAddressB cannot be an empty string", "ipAddressB");
    }

    var address1 = IPAddress.Parse(ipAddressA);
    var address2 = IPAddress.Parse(ipAddressB);

    if (address1.AddressFamily == address2.AddressFamily)
    {
        return (address1.Equals(address2));
    }

   // some code here to check mapped IP addresses

   return ...
}

Personally i would check the strings on NullOrWhitespace, but knowing you want to validate IP addresses, i recommend to use the IPAddress object for them which has an Parse and TryParse method. When that’s done it’s quite easy to create an static method for it which provides easy access.

Below i have written an example:

References:

using System;
using System.Linq;
using System.Net;

Code:

public static class IPAddressValidation
{
    public static bool Validate(string self)
    {
        IPAddress result;
        IPAddress.TryParse(self, out result);

        return result != null;
    }

    public static bool Validate(string[] self)
    {
        return self.All(Validate);
    }
}

Also, in some cases it might be sufficient to just throw an exception, when you are expecting it to be always valid for example, instead of user input. Validation methods don’t always have to return true or false, sometimes it’s such an essential value, that stopping the whole process by throwing the exception might be desired behavior.

Leave a Reply

Your email address will not be published. Required fields are marked *