简体   繁体   中英

How to avoid many if-else statements

I have a class like this..

Class A {

    public void someNullCheckingMethod(Student stu) {

        if (stu.getName() != null) {
            String name = stu.getName();
        } else {
                // get name from other source(its something like 
                // fallback)
               }

        if (stu.getId() != null) {
            String id = stu.getId();
        } else {
                // get id from other source(its something like 
                // fallback)
               }

        if (stu.getAddress() != null) {
            String address = stu.getAddress();
        } else {
                // get address from other source(its something like 
                // fallback)
               }

        if (stu.getCity() != null) {
            String city = stu.getCity();
        } else {
                // get city from other source(its something like 
                // fallback)
               }

        if (stu.getGender() != null) {
            String gender = stu.getGender();
        } else {
                // get gender from other source(its something like 
                // fallback))
               }
    }
}

is there a way to avoid too many if statements? As you can see here I am checking null condition for each property but i don't want many checks to get desired result just want to reduce if conditions as well as want to get same desired result whatever I will get by putting all these if conditions.

Since you don't provide any context there's a few things you can do based on some assumptions.

Assumption one:
if the values are initialized as null

String name;
// or
String name = null;

Then you can just assign the values and ignore if the fields are null or not since the class members are null already.

String name = stu.getName(); // String name = "Some Name" OR null, depending on return value of method

Assumption two:
If you just want to avoid the if statements you can go with ternary operators

String name = stu.getName() != null ? stu.getName() : null; // Or some other default value

There are a few other methods that pops into my mind as well but without more context they are a bit useless at this point.

如果可以选择使用外部库,则应查看DozerMapStruct

You could at least reduce the "verbosity" with Optional .

String name;
if (stu.getName() != null) {
    name = stu.getName();
} else {
    name = "default"
}

Would become

String name = Optional.ofNullable(stu.getName()).orElse("default");

Th choice is yours to return an Optional directly from the POJO Student for any value that could be null .

This would give a cleaner solution :

String name = stu.getName().orElse("default");

If getName looks like :

public Optional<String> getName(){
    return Optional.ofNullable(name);
}

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