简体   繁体   中英

Java if condition code optimise

Kindly help me if there is any other easy way to optimise\/implement the code on the below possibilities. Variables like home, bill, reg can either be empty string("") or null depending on the scenario.

public void helloWorld()
{
    List items=new ArrayList();
    items.add("1"); // dummy list
    String home=""; // can be null or empty
    String bill=null;  // can be null or empty
    String reg="";  // can be null or empty
    if(!items.isEmpty())
    {
        System.out.println("List is not null");
        if(home==null&&bill==null&&reg==null) 
        {
            System.out.println("home is null");
            System.out.println("bill is null");
            System.out.println("reg is null");
        }
        if(home==null&&bill==null&&reg!=null)
        {
            System.out.println("home is null");
            System.out.println("bill is null");
            System.out.println("reg is not null");
        }
        if(home==null&&bill!=null&&reg==null)
        {
            System.out.println("home is null");
            System.out.println("bill is not null");
            System.out.println("reg is null");
        }
        if(home==null&&bill!=null&&reg!=null)
        {
            System.out.println("home is null");
            System.out.println("bill is not null");
            System.out.println("reg is not null");
        }
        if(home!=null&&bill==null&&reg==null)
        {
            System.out.println("home is not null");
            System.out.println("bill is null");
            System.out.println("reg is null");
        }
        if(home!=null&&bill==null&&reg!=null)
        {
            System.out.println("home is not null");
            System.out.println("bill is null");
            System.out.println("reg is not null");
        }
        if(home!=null&&bill!=null&&reg==null)
        {
            System.out.println("home is not null");
            System.out.println("bill is not null");
            System.out.println("reg is null");
        }
        if(home!=null&&bill!=null&&reg!=null)
        {
            System.out.println("home is not null");
            System.out.println("bill is not null");
            System.out.println("reg is not null");
        }
    }
    else
    {
        System.out.println("List is null");
    }
}

Just check home , bill , and reg individually, because they have no dependency with each other.

public void helloWorld()
{
    List items=new ArrayList();
    items.add("1"); // dummy list
    String home=""; // can be null or empty
    String bill=null;  // can be null or empty
    String reg="";  // can be null or empty
    if(!items.isEmpty())
    {
        System.out.println("List is not null");
        System.out.println( home == null ? "home is null" : "home is not null" );
        System.out.println( bill == null ? "bill is null" : "bill is not null" );
        System.out.println( reg == null ? "reg is null" : "reg is not null" );

    }
    else
    {
        System.out.println("List is null");
    }
}

As proposed just check each variable instead of listing all permutations

    if(home==null) 
    {
        System.out.println("home is null");
    }
    else
    {
        System.out.println("home is not null");
    }
    if (bill==null)
    {
        System.out.println("bill is null");
    }
    else
    {
        System.out.println("bill is not null");
    }
    ...
 public void helloWorld() {
        List items = new ArrayList();
        items.add("1"); // dummy list
        String home = ""; // can be null or empty
        String bill = null;  // can be null or empty
        String reg = "";  // can be null or empty
        if (!items.isEmpty()) {
            System.out.println("List is not null");
            System.out.println("home is " + (StringUtils.isEmpty(home) ? "null" : "not null"));
            System.out.println("bill is " + (StringUtils.isEmpty(bill) ? "null" : "not null"));
            System.out.println("reg is " + (StringUtils.isEmpty(reg) ? "null" : "not null"));
        } else {
            System.out.println("List is null");
        }
    }

According to your comment on other answer, you need to do some actions...

So here is an example of how you could reuse the code to do different actions according to the value of different strings:

public void helloWorld() {
    List items = new ArrayList();
    items.add("1"); // dummy list
    String home = ""; // can be null or empty
    String bill = null; // can be null or empty
    String reg = ""; // can be null or empty


    switch (validateInputString(home)) {
    case 1:
        //doAction 1;
        break;
    case 2:
        //doAction 2;
        break;
    case 3:
        //doAction 3;
        break;

    default:
        break;
    }

}

public int validateInputString(String toValidate){
    if(toValidate==null)
        return 1;

    if(toValidate.equals(""))
        return 2;


    return 3;
}

You could do this for every string, and something quite similar for the list...

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