Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Wouldn't something like the following work?</p> <pre><code>if (methodName.equals("set" + this.name) || methodName.equals("get" + this.name) || (isBoolean() &amp;&amp; methodName.equals("is" + this.name))) { ... } </code></pre> <p>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 <code>isBoolean</code> 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).</p> <p>Also there's a similar question here titled <a href="https://stackoverflow.com/questions/522919/is-this-a-reasonable-use-of-the-ternary-operator">"Is this a reasonable use of the ternary operator?"</a> One user had <a href="https://stackoverflow.com/questions/522919/is-this-a-reasonable-use-of-the-ternary-operator/522929#522929">the following to say</a>:</p> <blockquote> <p>The ternary operator is meant to return a value.</p> <p>IMO, it should not mutate state, and the return value should be used.</p> <p>In the other case, use if statements. If statements are meant to execute code blocs.</p> </blockquote> <p>Do note that I included parentheses around the expression containing '&amp;&amp;' for readability. They aren't necessary because <code>x &amp;&amp; y</code> is evaluated before <code>m || n</code>.</p> <p>Whether you choose to use it is up to you, but I tend to avoid it in favour of readability.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload