How not to name a boolean variable

Out of personal experience and reading other people’s code,  I have come to realize a small but very tricky issue about how NOT to name boolean variables or any kind of variable where the code to evaluate is usually in the form of a boolean expression.

The pitfall to avoid is :

Do not use a name with a negative meaning or connotation

Instead of naming it notpassed call it failed . Instead of calling it no_data_present call it data_absent

The reason is simple. While reading the code, a negative sounding boolean variable causes more confusion than required.

Example:

bool not_done = false;

Various ways of checking it :

if (not_done) { }

if {!not_done) {}

if (not not_done) then //VB style

while (not_done  == false)

Try reading the above lines to yourself and you will see sometimes it gets confusing to actually understand what the condition is checking for really.

By common programming convention when we use a conditional operator , we are generally trying to see something positive has happened, so if the variable itself sounds like it stands for something positive it makes for easier understanding of the code.

Lets try the above example with a positive sounding variable:

bool done = false;

Various ways of checking it :

if (done) { }

if {!done) {}

if (not done) then //VB style

while (done  == false)

I think the above statements are easier to understand now.

13 Comments

  1. I personally like when developers use the negative booleans because it makes the decision to fire them on the spot so much clearer…

  2. Using the “Is” prefix is good. I saw many times “rV” for “returned value” which sucks. My motto is to use variable names which are self explanatory. Personally, I never use “negations” for boolean variables (not_this, nonexistent, etc.).

  3. amrelgarhy: Using an Is prefix is an excellent idea. It forces you to use a positive action as a suffix. (Unless , of course you come up with something like IsNotActive)

  4. I’ve noticed that asking a negative question is pretty much a waste of time in English. “Did you not take your medicine?” Most non-programmers will say “yes” to mean they did take the medicine, but some will answer “yes” to mean they didn’t. With programmer’s it’s the other way round.

  5. Chase: Your observation is interesting. ‘Did you take your medicine’ is infinitely simpler to understand than ‘Did you not take your medicine’

  6. In your first examples, the suggested alternatives “failed” and “data_absent” still have a negative connotation. I submit that instead of the variable “notpassed” and your suggested alternative of “failed”, one should use “passed” and simply negate it (!passed) to mean failure. Similarly, instead of “no_data_present” or “data_absent”, use “data_present” and negate it (!data_present) to mean the absence of data.

    • @Richard Ernst – I agree with your suggestion. Basically the big picture is to name the variables in a way that leaves no confusion as to what it stands for.

1 Trackback / Pingback

  1. Cheap PHP Script

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.