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.
I personally like when developers use the negative booleans because it makes the decision to fire them on the spot so much clearer…
You mean, “only use positive variable names for booleans” as opposed to (essentially) “don’t use negative names for boolean variables” 😉
I don’t think that not using non-negatively connotative isn’t a bad idea. In other words, I completely agree!!!
In most of cases i use “Is” for bool variable eample: IsExsit, IsDone, IsSent, …
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.).
I ruby, unless you use:
if not not_done
#…
end
You can use:
unless not_done
#…
end
tsk tsk: 🙂 Point taken
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)
Tiago: Even with the unless keyword, we still end up in confusion when trying to understand what ‘unless not_done’ would mean.
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.
Chase: Your observation is interesting. ‘Did you take your medicine’ is infinitely simpler to understand than ‘Did you not take your medicine’
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.