1

This question already has an answer here:

I am writing a method in C# (SharePoint Services) which is supposed to return a SharePoint list name based on three conditions (Client, Country, and Location). There are multiple clients, countries and location, and the number is going to increase in the future.

Th method I wrote uses a series of "if" statements, nested into each other. It works fine, but I would like to think about maintainability of the method in future, and would really like to refactor it to something cleaner.

Can you give advice on any design patterns which would improve the code?

Here is the code:

if(client = "Client1")
{
    if(country = "Country1"
    {
        if(location = "Location1"
        {
           return ListMatchingTheCriteria;
        }
        if(location = "Location2"
        {
           return ListMatchingTheCriteria;
        }
        ...
    }
    if(country = "Country2"
    {
        if(location = "Location1"
        {
           return ListMatchingTheCriteria;
        }
        if(location = "Location2"
        {
           return ListMatchingTheCriteria;
        }
        ...
    }

}
if(client = "Client2")
{
    if(country = "Country1"
    {
        if(location = "Location1"
        {
           return ListMatchingTheCriteria;
        }
        if(location = "Location2"
        {
           return ListMatchingTheCriteria;
        }
        ...
    }
    if(country = "Country2"
    {
        if(location = "Location1"
        {
           return ListMatchingTheCriteria;
        }
        if(location = "Location2"
        {
           return ListMatchingTheCriteria;
        }
        ...
    }

}
...

marked as duplicate by gnat, Doc Brown c# Jan 28 at 17:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • there are lots of questions asking this if you search – Ewan Jan 28 at 10:02
  • 1
    This is easily solved using a Dictionary<(string client, string country, string location), IList<Whatever>> – David Arno Jan 28 at 10:13
2

In general a nested if is the clearest way to deal with this kind of thing.

However, if you have a structure to your conditions you can use a dictionary/hashmap etc to look up the result

private Dictionary<string key, ListMatchingTheCriteria value> options; //all the options go here

public GetResult(var whateverSelectionCritera)
{
    var key = whateverSelectionCritera.ToString(); //add together your various criteria into a unique value
    return options[key]; // or run the returned function etc
}

As I say, this is great when you have a simple pattern of criteria, but becomes complicated when you have exceptional cases.

There is also the classic OOP inheritance solution:

public class DefaultOption
{
    public virtual void Run();
}

public class OptionA : DefaultOption
{
   public override void Run();
}

Obviously this has the problem of how you select which class to instantiate initially.

RE: duplicate answers. I have a search and none of the duplicates seem to have definitive accepted answers. maybe we should have a community page of some kind.

  • Thanks Ewan, the solution using Dictionary is going to be best for my case! :) – Emil Gurbanov Jan 28 at 11:02
  • 1
    You're missing '>' in definition of 'options' dictionary – Jon Barker Jan 28 at 20:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.