Farid Ahmad
Farid Ahmad

Reputation: 21

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.

Upvotes: 1

Views: 479

Answers (3)

AxelH
AxelH

Reputation: 14582

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);
}

Upvotes: 0

Brenin
Brenin

Reputation: 195

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.

Upvotes: 2

tbsalling
tbsalling

Reputation: 4555

If using an external library is an option, then you should take a look at Dozer or MapStruct.

Upvotes: 0

Related Questions