Manjunath
Manjunath

Reputation: 13

Reducing conditions within a if statement

I have a code snippet as below. Here there is a nested if else loop as well as multiple conditions [all different parameters]. What is the best way to optimize this.

    if(request!=null && !StringUtils.isBlank(request)) {
        if(request.getFirstName()!=null && !StringUtils.isBlank(request.getFirstName())
                && request.getLastName()!=null && !StringUtils.isBlank(request.getLastName())
                && request.getAge()!=null && !StringUtils.isBlank(request.getAge())
                && request.getAddress()!=null && !StringUtils.isBlank(request.getAddress())
                && request.getPhoneNumber()!=null && !StringUtils.isBlank(request.getPhoneNumber())) {
            return true;
        }else {
            return false;
        }
    }else {
        return false;
    }

I had thought of using switch case and for loop as well but all the conditions are based on different variables, I didn't see it as compatible.

Upvotes: 1

Views: 220

Answers (4)

vsfDawg
vsfDawg

Reputation: 1527

You have a few syntax errors

  • You are passing request to StringUtils but it doesn't appear to implement CharSequence
  • You are using !! instead of !
  • You invocation of the get methods does not include the () to mark it as methods.

Although not an error, you do not need nested if-statements here. Using unnecessary if-else-blocks can make it harder to decipher what the code is doing. It can, however, allow for comments to describe why certain conditions are being checked or whatever. None of that seems relevant here. In fact, you can pass the result of the boolean operation without any if-statement. Using if-statements that return true or false looks like this.

if (<condition-is-true?>) return true 
else return false;

Which can be simplified to...

return <condition-is-true?>;

Further, assuming you are using using Apache StringUtils, you do not need to check for null first - the isEmpty(CharSequence) method does that. Additionally, StringUtils includes the isAnyEmpty(CharSequence...) method so you can pass all of the Strings at once.

return request != null && !StringUtils.isAnyEmpty(
      request.getFirstName(),
      request.getLastName(),
      request.getAge(),
      request.getAddress(),
      request.getPhoneNumber());

Upvotes: 0

Nikolai  Shevchenko
Nikolai Shevchenko

Reputation: 7521

If you don't use commons-lang dependency you can simply use Stream API (Java 8+)

Boolean allNonBlank = Stream.of(
                        request.getFirstName(),
                        request.getLastName(),
                        request.getAge(),
                        request.getPhoneNumber())
                .allMatch(it -> it != null && !String.isBlank(it));

Upvotes: 0

Stanislav Bashkyrtsev
Stanislav Bashkyrtsev

Reputation: 15308

StringUtils from commons-lang already has a method which accepts an array of Strings. It will check for null or empty or blank strings. So all your checks boil down to:

    return !(request == null || StringUtils.isAnyBlank(
             request.getFirstName, request.getLastName,
             request.getAge, request.getPhoneNumber));

Upvotes: 7

Kapil Thakkar
Kapil Thakkar

Reputation: 870

You can try StringUtils.isAnyBlank(). Please refer attached link.

isAnyBlank : https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#isAnyBlank-java.lang.CharSequence

Upvotes: 0

Related Questions