Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm old school, so I believe in only one entry and exit point for a method or function...</p> <p>The essence of your problem comes down to the compilers inability to guarantee that any one <code>if</code> statement "may" be meet (you and I know that the last one should be, but the compiler won't take that risk).</p> <p>So instead of having a <code>return</code> for every condition, you could define a single <code>return</code> variable and change it's value based on your needs, for example...</p> <pre><code>String getMaturityLevel(int age) { String maturity = "antique"; if(age &lt; 2) maturity = ("a baby"); else if(age &lt; 4) maturity = ("a toddler"); else if(age &lt; 13) maturity = ("a child"); else if(age &lt; 20) maturity = ("a teenager"); else if(age &lt; 35) maturity = ("a young adult"); else if(age &lt; 65) maturity = ("middle aged"); else if(age &lt; 100) maturity = ("geriatric"); return maturity; } </code></pre> <p>Now, in this small piece of code, it may not make much difference, but when you deal with methods that are longer or have multiple compound if/loop statements, a single <code>return</code> statement anywhere in the code can ruin your understanding of what you think the method is doing...it's a nit pick, but anything that makes my life easier ;)</p> <p>I would also encourage you to use <code>{...}</code> around your <code>if</code> statements, it will stop you from doing something like...</p> <pre><code>else if(age &lt; 100) maturity = ("geriatric"); maturity = "Why is the returned value for every condition?"; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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