Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem occurs when, later in the project, more conditions are added - one of the projects I'm currently working on has steadily collected conditions over time (and then some of those conditions were moved into struts tags, then some to JSTL...) - one negative isn't hard to read, but 5+ is a nightmare, especially when someone decides to reorganize and negate the whole thing. Maybe on a new project, you'll write:</p> <pre><code>if (authorityLvl!=Admin){ doA(); }else{ doB(); } </code></pre> <p>Check back in a month, and it's become this:</p> <pre><code>if (!(authorityLvl!=Admin &amp;&amp; authorityLvl!=Manager)){ doB(); }else{ doA(); } </code></pre> <p>Still pretty simple, but it takes another second.<br> Now give it another 5 to 10 years to rot.<br> (x%2!=0) certainly isn't a problem, but perhaps the best way to avoid the above scenario is to teach students not to use negative conditions as a general rule, in the hopes that they'll use some judgement before they do - because just saying that it could become a maintenance problem probably won't be enough motivation. </p> <p>As an addendum, a better way to write the code would be:</p> <pre><code>userHasAuthority = (authorityLvl==Admin); if (userHasAuthority){ doB(); else{ doA(); } </code></pre> <p>Now future coders are more likely to just add "|| authorityLvl==Manager", userHasAuthority is easier to move into a method, and even if the conditional is reorganized, it will only have one negative. Moreover, no one will add a security hole to the application by making a mistake while applying De Morgan's Law.</p>
    singulars
    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.
    3. 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