-1

Consider this code -

if(enable) {
  if(enableCar) {
    // do something
  } else if(enableComputer) {
    // do something
  } else if(enableTV) {
    // do something
  } else {
    otherChoice();
  }
} else {
  otherChoice();
}

// More code here so return isn't possible

How should i avoid so much else statements, i can't remove the if(enable) because a carEnable can return true while enable itself will be false.

I can't use return as i have more code below

So how should i avoid so much else statements?

Of course it's just an example :)

Thanks

julian
  • 4,634
  • 10
  • 42
  • 59
  • Is it intended that if `enable` is true and `enableCar` and `enableComputer` and `enableTV` are all false that `otherChoice()` is run three times? – Golden Dragon Feb 28 '14 at 14:05
  • "just an example" <-- without the context, no real meaningful answer can be given. Maybe your method needs refactoring, of a particular class, or several classes, or... Right now, no one can tell – fge Feb 28 '14 at 14:08
  • If enableCar, enableComputer and enableTV are all false you run the otherChoice code three times? And the three boolean are mutually exclusive or you could have all three set to true? – Steve Feb 28 '14 at 14:13

6 Answers6

2

You have not made your requirement completely clear but i think your requirement needs a switch block.

[Edit]: Since later the tag for Java was added - what you can do is to use an enum for this.

Sadique
  • 22,572
  • 7
  • 65
  • 91
  • 4
    Should mention here that, in Java, switch does not work on boolean. http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html – Ali Cheaito Feb 28 '14 at 14:08
1

You could add your options to a enum and pass it to the switch.

public enum EnableOptions
{
   enableCar,

   enableComputer,

  enableTV


}

Then create a variable for the option and pass that to the switch:

EnableOptions option = EnableOptions.enableCar;



if(enabled)
{

    switch(option)
    {
     case EnableOptions.enableCar:


           //method
           break;
     case EnableOptions.enableComputer:

          // methods
          break;

        etc... for all of your options

      default:

        //something
        break;

    }

}
Felix Castor
  • 1,598
  • 1
  • 18
  • 39
0

you can also try using else if and one else at the end

    if (enableCar) {
        // do something
    }
    else if (enableComputer) {
        // do something
    }
    else if (enableTV) {
        // do something
    } else {
        otherChoice();
    }
upog
  • 4,965
  • 8
  • 42
  • 81
0

The pseudo code lacks to much information. Sometimes cascades of if ... else can be avoided by using polymorphism. Have a look at this thread. And read this article about replacing conditionals with polymorphism.

Community
  • 1
  • 1
Axel
  • 13,939
  • 5
  • 50
  • 79
  • I guess you are right.. ok then i will close / delete this post. so people won't get confused... hell i can't close this. – julian Feb 28 '14 at 14:14
  • You don't have to close it yourself. If the mods think the post is of no value, they will delete it. Actually I think the question (or better the answers) might be of use for some. – Axel Feb 28 '14 at 14:20
0

Actually good structure-analysis is a general way, how to get rid off the if-else statements, because structure itself contains some information, you have to decide by your if-else statements.

So you should think which classes you should need and which responsibility and varibales they have. Your example is too abstract, so I cant tell you exactly what you should do.

For example - you should consider command pattern : http://www.oodesign.com/command-pattern.html

Then you can just using "commad.do()" and command itself knows, what to do, because each command has its own functionability.

libik
  • 22,239
  • 9
  • 44
  • 87
-2

You could create a switch statement

if(enable)
{
    switch()
    {
         case enableCar:
         // do some code
         break;
         case enableComputer:
         //do some code
         break; 
    }
}
Kbaugh
  • 1
  • 1
  • Ur code shows that the result from the method could be a Boolean output. A switch statement would be an option for this. I do not see a problem with my solution. However it does not state the language used so this might not be ok for what you are looking for, but could be helpful to someone else – Kbaugh Feb 28 '14 at 14:12