Wouldn't something like the following work?
if (methodName.equals("set" + this.name)
|| methodName.equals("get" + this.name)
|| (isBoolean() && methodName.equals("is" + this.name))) {
...
}
It's more readable than the way in which you used the ternary operator and certainly easier to understand. It also has the advantage that it can avoid an unnecessary method call to the isBoolean
method (it has either 1, 2 or 4 method calls whereas yours always has either 1 or 3; the performance gain/loss is probably too minute to notice).
Also there's a similar question here titled "Is this a reasonable use of the ternary operator?" One user had the following to say:
The ternary operator is meant to
return a value.
IMO, it should not mutate state, and
the return value should be used.
In the other case, use if statements.
If statements are meant to execute
code blocs.
Do note that I included parentheses around the expression containing '&&' for readability. They aren't necessary because x && y
is evaluated before m || n
.
Whether you choose to use it is up to you, but I tend to avoid it in favour of readability.