If there is still redundant code in a simpler form

What is the best way to represent the following condition if else?

        if ((!StringUtils.isEmpty(A) && !StringUtils.isEmpty(B)) {
            System.out.println("Case 1");
        } else if ((!StringUtils.isEmpty(A) && StringUtils.isEmpty(B)) {
            System.out.println("Case 2");
        } else if ((StringUtils.isEmpty(A) && !StringUtils.isEmpty(B)) {
            System.out.println("Case 3");
        } else if ((StringUtils.isEmpty(A) && StringUtils.isEmpty(B)) {
            System.out.println("Case 4");
        } else {
            System.out.println("End");
        }
+4
source share
3 answers

Assuming that Case 1and Case 4do not have the same condition, which excludes the possibility End, you can do this:

if (StringUtils.isEmpty(A)) {
    if (StringUtils.isEmpty(B)) {
        System.out.println("Case 4");
    } else {
        System.out.println("Case 3");
    }
} else {
    if (StringUtils.isEmpty(B)) {
        System.out.println("Case 2");
    } else {
        System.out.println("Case 1");
    }
}

Or you can use the switch statement with a little bit of math:

switch ((StringUtils.isEmpty(A) ? 2 : 0) | (StringUtils.isEmpty(B) ? 1 : 0)) {
    case 0: // !empty(A) & !empty(B)
        System.out.println("Case 1");
        break;
    case 1: // !empty(A) & empty(B)
        System.out.println("Case 2");
        break;
    case 2: // empty(A) & !empty(B)
        System.out.println("Case 3");
        break;
    default: // empty(A) & empty(B)
        System.out.println("Case 4");
}
+4
source

This is the clearest way I can think of to write your example.

Note that I used the standard Java convention for lowercase letters for identifiers. If you really specified your variables Aand Bfor some reason you can use more standard names.

import static org.apache.commons.lang3.StringUtils.isEmpty;

...

if (isEmpty(a)) {
    if (isEmpty(b)) {
        System.out.println("Case 4");
    } else {
        System.out.println("Case 3");
    }
} else {
    if (isEmpty(b)) {
        System.out.println("Case 2");
    } else {
        System.out.println("Case 1");
    }
}
+3

.

, ,

    import static <pkg>.StringUtils.isEmpty;

    if (bothEmpty(A,B)) {
        System.out.println("Case 4");
    } else if (isEmpty(A)) {
       System.out.println("Case 2");
    } else if (isEmpty(B)) {
        System.out.println("Case 3");
    } else  {
       System.out.println("Case 1");
    }

  boolean bothEmpty(String A, String B){
     return isEmpty(A) && isEmpty(B);
  }

I usually return a value if the condition is satisfied. This avoids some duplication cases and nested if-else cases.

 public originalAction(String A, String B){
     System.out.println(value(A,B));
 }
 String value(String A, String B){
    if (bothEmpty(A,B))  return "Case 4";
    if (isEmpty(A)) return "Case 2";
    if (isEmpty(B)) return "Case 3";
    return "Case 1";
}
boolean bothEmpty(String A, String B){
   return isEmpty(A) && isEmpty(B);
}
+1
source

Source: https://habr.com/ru/post/1687270/


All Articles