Home > OS >  How do I validate the same variable multiple times?
How do I validate the same variable multiple times?

Time:08-03

Imagine that I want to validate if the object is null and I have 3 methods that will help me, such:

  • GetCompanyBySystemId();
  • GetCompanyById();
  • GetCompanyByName();

So, first I'll try to get the Company by system Id, if it's null I'll try to get it by Id, and, last but not least, I'll try to get it by Name.

My question is, what is the best practice to validate the company without doing this:

var company = GetCompanyBySystemId(string);

if (company == null)
{
  company = GetCompanyById(string);
}

if (company == null)
{
  company = GetCompanyByName(string);
}
...

CodePudding user response:

You may use the null-coalescing-operator:

var company = GetCompanyBySystemId(@string) ?? 
              GetCompanyById(@string) ??
              GetCompanyByName(@string);

Also notice I added @ as string is a reserved keyword and therefor not allowed as variable-name.

CodePudding user response:

You can try something like this but your code is not that bad as you think.

var company = GetCompanyBySystemId(string)
    ?? GetCompanyById(string)
    ?? GetCompanyByName(string);

basically ?? means - if left side is null execute right side

CodePudding user response:

I always like the Try* paradigm (like int.TryParse) when you are stating that something may or may not be available, and you won't know at runtime:

public static void Main()
{
    string id = "test";
    object company = null;

    if(!TryGetCompanyBySystemId(id, out company) 
        && !TryGetCompanyById(id, out company) 
        && !TryGetCompanyByName(id, out company))
    {
        //we've failed entirely
    }
    else
    {
        //we got a company 
    }
}

public static bool TryGetCompanyBySystemId(string id, out object company)
{
    company = null;

    //locate company and return true if found

    return false;
}

public static bool TryGetCompanyById(string id, out object company)
{
    company = null;

    //locate company and return true if found

    return false;
}

public static bool TryGetCompanyByName(string id, out object company)
{
    company = null;

    //locate company and return true if found

    return false;
}

CodePudding user response:

If you often have to seach for Company in such a way, I suggest extrcating a method in which we can hide all the details:

public static Company GetCompany(string idOrName) {
  if (value is null)
    throw new ArgumentNullException(nameof(idOrName));

  var result = GetCompanyBySystemId(idOrName) ??
               GetCompanyById(idOrName) ??
               GetCompanyByName(idOrName);

  return result != null
    ? result
    : throw new ArgumentException($"Company with id or name {idOrName} is not found.", 
                                    nameof(idOrName));
}

Then whenever you want a company just get it:

Company company = GetCompany(@string);

In case not finding company is normal situation, you can implement TryGetCompany:

public static bool GetCompany(string idOrName, Company company) {
  // Still exceptional situaltion 
  if (value == null)
    throw new ArgumentNullException(nameof(idOrName));

  company = GetCompanyBySystemId(idOrName) ??
            GetCompanyById(idOrName) ??
            GetCompanyByName(idOrName);

  return company != null;
}

usage

if (TryGetCompany(@string, out var company)) {
  // company with id or name string has been found and put into company 
}
else {
  // company with id or name string has not been found
}
  • Related