Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 713263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:56:29+00:00 2026-05-14T04:56:29+00:00

I remember many years back, when I was in school, one of my computer

  • 0

I remember many years back, when I was in school, one of my computer science teachers taught us that it was better to check for ‘trueness’ or ‘equality’ of a condition and not the negative stuff like ‘inequality’.

Let me elaborate – If a piece of conditional code can be written by checking whether an expression is true or false, we should check the ‘trueness’.

Example: Finding out whether a number is odd – it can be done in two ways:

if ( num % 2 != 0 )
{
  // Number is odd
}

or

if ( num % 2 == 1 )
{
  // Number is odd
}

(Please refer to the marked answer for a better example.)

When I was beginning to code, I knew that num % 2 == 0 implies the number is even, so I just put a ! there to check if it is odd. But he was like ‘Don’t check NOT conditions. Have the practice of checking the ‘trueness’ or ‘equality’ of conditions whenever possible.’ And he recommended that I use the second piece of code.

I am not for or against either but I just wanted to know – what difference does it make? Please don’t reply ‘Technically the output will be the same’ – we ALL know that. Is it a general programming practice or is it his own programming practice that he is preaching to others?

NOTE: I used C#/C++ style syntax for no reason. My question is equally applicable when using the IsNot, <> operators in VB etc. So readability of the ‘!’ operator is just one of the issues. Not THE issue.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:56:30+00:00Added an answer on May 14, 2026 at 4:56 am

    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:

    if (authorityLvl!=Admin){
     doA();
    }else{
     doB();
    }
    

    Check back in a month, and it’s become this:

    if (!(authorityLvl!=Admin && authorityLvl!=Manager)){
     doB();
    }else{
     doA();
    }
    

    Still pretty simple, but it takes another second.
    Now give it another 5 to 10 years to rot.
    (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.

    As an addendum, a better way to write the code would be:

    userHasAuthority = (authorityLvl==Admin);
    if (userHasAuthority){
     doB();
    else{
     doA();
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Many years back, I was told that Javascript was harmful, and I remember being
I can remember that many years ago (in 2005) I was using BackgroundWorker in
I remember many years ago using a program that would allow Classic ASP websites
I've got two tables (articles and tags) that have a one-to-many relationship. I remember
I remember reading something about it a few years back but I don't remember
I remember many years ago they were teaching us to create local variables outside
I used a script many years ago that was basically and administrative backend where
I remember a few years back, when I had a web-site I wanted to
I have many projects I work on normally, but I have one that consumes
Many years ago I remember a fellow programmer counselling this: new Some::Class; # bad!

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.