1

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.

9
  • 2
    Possible duplicate of Avoiding != null statements Commented Aug 23, 2018 at 10:15
  • 2
    Where have you put those if statements? The given example will not work since they are in the class, but not in a method or a constructor. Commented Aug 23, 2018 at 10:15
  • 2
    Explaining the goal of your class, and maybe providing a bit more of context would help to come with a solution. Commented Aug 23, 2018 at 10:18
  • 1
    Hello. Welcome to SO. As it is, your question is unclear. You should consider providing a more detailed example. To do so, I suggest you to take a look to the following documentation : How to create a Minimal, Complete, and Verifiable example Commented Aug 23, 2018 at 10:22
  • 1
    And why would you like to avoid the if statements? Does that have any thing to do with a coding rule violation? Or is it a simple matter of taste? In case it is, you can move on to another language such as Kotlin since Java is meant to be so ;) Commented Aug 23, 2018 at 10:50

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.