Search code examples
javareadabilityif-statement

if-else structure


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


Solution

  • 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.