开发者

if-else structure

开发者 https://www.devze.com 2022-12-26 13:19 出处:网络
I have these long statements that I will refer to as x,y etc. here. My conditional statements\' structure goes like this:

I have these long statements that I will refer to as x,y etc. here. My conditional statements' structure goes like this:

if(x || y || z || q){
    if(x)开发者_开发技巧
       do someth
    else if (y)
       do something

    if(z)
       do something
    else if(q)
       do something
}
else
    do smthing

Is there a better, shorter way to write this thing? Thanks


I don't see a big problem with how you write it now. I do recommend using curly braces even for single statement if-blocks. This will help you avoid mistakes in case you have to add more code lines later (and might forget to add the curly braces then). I find it more readable as well. The code would look like this then:

if (x || y || z || q) {
    if (x) {
       do something
    } else if (y) {
       do something
    }

    if (z) {
       do something
    } else if (q) {
       do something
    }
} else {
    do something
}


Another variant that avoids the multiple checks and the errorprone complex logical expressions might be:

boolean conditionhandled = false;
if (x) {
   do something
   conditionhandled = true;
} else if (y) {
   do something
   conditionhandled = true;
}

if (z) {
   do something
   conditionhandled = true;
} else if (q) {
   do something
   conditionhandled = true;
}

if (!conditionhandled) {
   do something
}


This seems pretty clear to me (and clear is good).

What you can do is first evaluate x,y,z and q and store those as variables so you don't have to do that twice.


Maybe this is a little easier to read. But now you will perform one extra check. If it is not mission critical code then maybe you can use the following:

if (x)
  do something;
else if (y)
  do something;

if (z)
  do something;
else if(q)
  do something;

if !(x || y || z || q)
  do something completely different.


I'm not recommending the following, in fact, I think what you got is fine, but:

s = true;
if (x) {
    do something;
    s = false;
} else if (y) {
    do something;
    s = false;
}
if (z) {
    do something;
    s = false;
} else if (q) {
    do something;
    s = false;
}

if (s) {
    so something;
}


Can you make some assumptions about x,y,z,q? e.G. just one of them can be true. Than you could see it as a State

enum State {
X{
  void doSomething(){
    doItTheXWay();
  }  
},
Y{
  void doSomething(){
    doItTheYWay();
  }  
},
Z{
  void doSomething(){
    doItTheZWay();
  }  
},
Q{
  void doSomething(){
    doItTheQWay();
  }  
};
  void doSomething(){

  }
}

and in your code where you used the if statements

you could assign a state and just do the right thing

State state = getAState();
state.doSomething();

In case you don't like enums State could be an Interface and X to Q could be implementing classes. The benefits in this case are in multiple usage of the same if else construct. Say some codelines later you would begin with

if(x)
  do_the_next_thing_with_X();
...

or you could just extend your enum with another function and make one single call

state.doTheNextThing();
0

精彩评论

暂无评论...
验证码 换一张
取 消