I have some simple logic to check if the field is valid:
private boolean isValidIfRequired(Object value) {
return
(required && !isEmpty(value)) || !required;
}
it tells that the field is valid if it’s either required and not empty or not required.
I don’t like this required || !required part. Something with just required would be better.
How do I simplify this method to make it more readable and simple?
How ’bout:
or (thanks, @Peter Lawrey)
In either case, if
requiredisfalse, the||or&&expression will short-circuit andisEmptywill never be called. Ifrequiredistrue, the second half of the||or&&will be evaluated, callingisEmptyand returning the (inverted) result of that call.