简体   繁体   中英

Is there any cleaner way to write multiple if-statements in Java

I have an if-else structure in Java as follow:

                    if (A || B || C){
                        if (A){
                            //Do something
                        }
                        if (B){
                            //Do something
                        }
                        if (C){
                            //Do something
                        }
                    } else {
                        //Do something
                    }

I want to know if there is any cleaner and easier way to replace this?

If A,B and C are conditions which are expensive to evaluate, you could use an additional flag to make sure they are only evaluated once:

boolean found = false;
if (A) {
    //Do something
    found = true;
}
if (B){
    //Do something
    found = true;
}
if (C){
    //Do something
    found = true;
}
if (!found) {
    //Do something
}

Otherwise (ie if they are not expensive to evaluate), I'd keep your current conditions.

Yes.

if (A) {
  // do A
}
if (B) {
  // do B
}
if (C) {
  // do C
}
if (! (A || B || C)) {
  // do "neither A or B or C"
}

Whilst I think what you have is fine, you could do it like this:

boolean d = false;
if (d |= A) { ... }
if (d |= B) { ... }
if (d |= C) { ... }
if (!d) {
  // What to do if A..C are all false.
}

This will set d to true if any of the conditions are matched ( d is for "did something").

Just one more possible solution:

public class ABC {

    private static final Logger log = LoggerFactory.getLogger(ABC.class);

    @Test
    public void abc() {
        boolean A = true;
        boolean B = false;
        boolean C = true;

        boolean abcProcessed = false;

        abcProcessed |= process(A, () -> {
            // Do something for A
            log.debug("A");
        });
        abcProcessed |= process(B, () -> {
            // Do something for B
            log.debug("B");
        });
        abcProcessed |= process(C, () -> {
            // Do something for B
            log.debug("C");
        });

        if (!abcProcessed) {
            // Do something for !(A||B||C)
            log.debug("!(A||B||C)");
        }
    }

    private boolean process(boolean flag, DoSomethingInterface doSomethingInterface) {
        // check condition
        if (flag) {
            // execute code specific for condition
            doSomethingInterface.doSomething();
            // return true (processed)
            return true;
        }

        // return false (not processed)
        return false;
    }

    public interface DoSomethingInterface {

        // code specific for condition
        void doSomething();
    }
}

An idea is to move condition checking and related code to separate method.

I think you should use else if conditions:

if(A){

}else if(B){

}else if(C){

}else if(D){

}

But, if you have many conditions like A,B,C etc it feels that something wrong with logic of your program

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM