Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TOO MANY if (obj is thisObj) statements

Tags:

c#

I currently have method which is trying to find out what the obj is it recieved. It knows is on a certain interface, for example IService but I have code which looks at it and tries to tell me is it is for example Service1 or Service2. I currently a lot of if(obj is thisObj) style statements, what would be the best solution to make this code pretty?

here is a sample of what exactly I have:

    public void DoSomething(IService service)
    {
        if (service is Service1)
        {
            //DO something
        }

        if (service is Service2)
        {
            //DO something else
        }           
    }

now having two isnt too much of a bad thing, but I am looking at having probably 20+ of these which just becomes awful to use.

Any ideas?


ok further details I think are needed and here they are:

prior to this method I have another method which is recieving a xml doc, which it them deserializes into the interface IService, so we have something like this:

    private static void Method(InnerXml)

    {

            var messageObj = (IServiceTask)XmlSerialization.Deserialize(typeof(IServiceTask), InnerXml);

            var service = GetService(messageObj);
            service.PerformTask(xmlDoc);

    }

    private static IService GetService(IServiceTask messageObj)
    {
        var service = new IService ();

        if (messageObj is Task1)
        {
            service = (SomeService)messageObj;
        }

        if (messageObj is Task2)
        {
            service = (SomeOtherService)messageObj;
        }
        return service ;
    }

Hopefully that makes it a bit clearer.

like image 905
JamesStuddart Avatar asked Oct 26 '10 13:10

JamesStuddart


People also ask

How many if statements is too many?

You can nest up to 7 IF functions to create a complex IF THEN ELSE statement. TIP: If you have Excel 2016, try the new IFS function instead of nesting multiple IF functions.

What happens if multiple if statements are true?

So if you have two else if statements that would both be true, only the top one would be executed.

Should you avoid using if statements?

There is nothing wrong with using if-statements, but avoiding them can sometimes make the code a bit more readable to humans. This is definitely not a general rule as sometimes avoiding if-statements will make the code a lot less readable. You be the judge. Avoiding if-statements is not just about readability.


2 Answers

Can you change IService ?

Add method DoSomething() and implement it in all the services.

like image 108
Itay Karo Avatar answered Nov 03 '22 13:11

Itay Karo


Well, it depends on what the //DO something lines are doing. In some cases it would be appropriate to declare a method in the service interface and put the logic for those operations in the services themselves.

Sometimes, on the other hand, it's code which the service itself ought not to know about - at which point life becomes distinctly uglier :( Sometimes this sort of thing is really hard to avoid. I've occasionally found that a mixture of generics and lambda expressions help, e.g.

ConditionallyExecute<Service1>(service, s1 => s1.CallSomeService1Method());
ConditionallyExecute<Service2>(service, s2 => s2.CallSomeService2Method());
...

where ConditionallyExecute is something like:

private void ConditionallyExecute<T>(object obj, Action<T> action)
    where T : class
{
    T t = obj as T;
    if (t != null)
    {
       action(t);
    }
}

... but I'm not really happy when I do that :(

like image 38
Jon Skeet Avatar answered Nov 03 '22 13:11

Jon Skeet