Home > Software design >  How to simplify the code if there are many logical operators "or - ||"
How to simplify the code if there are many logical operators "or - ||"

Time:01-26

How to simplify the code if there are many logical operators "||"

    if (Boolean.TRUE.equals(fileBeforeCompressTmp1Passport > 10485760
            || fileAfterCompressionTmp1Passport < 10240
            || fileBeforeCompressTmp1Passport < fileAfterCompressionTmp1Passport

            || fileBeforeCompressTmp1Medical > 10485760
            || fileAfterCompressionTmp1Medical < 10240
            || fileBeforeCompressTmp1Medical < fileAfterCompressionTmp1Medical

            || value.phone().toString().length() != 10
            || validationRegExp.onlyNumbersRegExp(value.phone().toString())
            || serviceJpa.existsLogisticsPersonByPhone(value.phone())

            || value.email().length() < 8
            || value.email().length() > 58
            || validationRegExp.emailValidationRegExp(value.email())

            || value.password().length() < 6
            || value.password().length() > 24
            || validationRegExp.passwordValidationRegExp(value.password())

            || value.surname().length() < 1
            || value.surname().length() > 45
            || validationRegExp.onlyLettersCyrillic(value.surname())

            || value.name().length() < 1
            || value.name().length() > 45
            || validationRegExp.onlyLettersCyrillic(value.name())

            || value.middleName().length() < 1
            || value.middleName().length() > 45
            || validationRegExp.onlyLettersCyrillic(value.middleName())

            || value.dateBirth().toString().length() != 10
            || validationRegExp.validationDateRegExp(formattedString)

            || value.numberPassport().toString().length() != 10
            || validationRegExp.onlyNumbersRegExp(value.numberPassport().toString())
            || serviceJpa.existsLogisticsPersonByNumberPassport(value.numberPassport())

            || value.region().length() != 7
            || validationRegExp.onlyLettersCyrillic(value.region())

            || value.city().length() < 2
            || value.city().length() > 25
            || validationRegExp.onlyLettersCyrillic(value.city())

    ) {

        Files.deleteIfExists(ofPassport);
        return ResponseEntity.ok(new MessageResponse(HttpStatus.OK.value(), STATIC_OK));

    }

CodePudding user response:

Best practice would likely be to break your '||/or' statements into their own boolean sub-methods, grouped by related checks. I.E. grouping all the email checks in a method.

boolean methodName() 

{

if (email.length() < 8){
  return true
} //do this for each if, return false at the bottom.

}

If one of them returns true, the method returns true. Do this for each field, and then you can '||' them together in the main call and it'll look a bit cleaner. Depending on what you're trying to accomplish with your code, this may not be the most efficient, but it'll look cleaner.

CodePudding user response:

why not put all those into a arraylist and then method for each

  •  Tags:  
  • java
  • Related