EternalSheriff
EternalSheriff

Reputation: 3

How do I validate the same variable multiple times?

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

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);
}
...

Upvotes: 0

Views: 118

Answers (3)

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37000

You may use the null-coalescing-operator:

var company = GetCompanyBySystemId(@string) ?? 
              GetCompanyById(@string) ??
              GetCompanyByName(@string) ?? 
              throw new InvalidOperatonException($"No company found for { @string }");

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

Upvotes: 3

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186698

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:

// Just find me the company by string and don't make we think
// names, ids etc. things 
Company company = GetCompany(@string);

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

public static bool TryGetCompany(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
}

Upvotes: 1

Jonesopolis
Jonesopolis

Reputation: 25370

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;
}

Upvotes: 2

Related Questions