I have a list of types and ids. my scenario is when type is type 1, I want to make type 1 ID cannot be null. when the type is something else type 1 can be null. when this happens, I want to return true else false.
What I did is
boolean assertValue = false;
if ( !(type == types.TYPEONE) || typeOneID == null ) {
assertValue = true;
} else if ( type == types.TYPEONE && typeOneID != null ) {
assertValue = true;
}
return assertValue;
Is there a cleaner way of writing this in JAVA ? any idea is appreciated
CodePudding user response:
May be You Can Change Your else if(type == types.TYPEONE && typeOneID != null)
to else if(type == types.TYPEONE || typeOneID != null)
. So that you can't get null. Simply Change your else if
Condition Logical Operator's &&
to ||
.
CodePudding user response:
Assuming the expressions in your code are what you intend (see comments on the question), the whole conditional statement can be simplified using the distributive property to:
return type != types.TYPEONE && typeOneID == null;