Misko Hevery

Recent site activity

Blog Entries‎ > ‎

2008-01-15 Negative Questions Give me a Headache

posted May 6, 2008 10:23 PM by Misko Hevery

When naming flags, or any variables for that matter, use positive concepts so that the user has easier time reading the code.

Let me demonstrate how negative questions are difficult to understand. I have a GPS which routinely asks me this questions.

Your destination route goes through a toll road. Do you not want to avoid the tolls: [ YES ] [ NO ] 

If I see this while standing I want to throw the GPS out the window, and when I am driving, I just barely mange not to have an accident. I really have to think hard as to what the YES will do. The problem here is that YES represents positive while the question has a double negative. "Avoid" is already a negative concept, and adding "not" only makes it worse.

But bad UI is not the only problem, sometimes bad colloquialism is at fault.

"Do you mind if I eat the last piece of chocolate?"

  • "NO!" My initial reaction is that I am not supposed to eat it. "No!", wait, it was negative question so no means yes, ohh, great I can eat it.
  • "YES!" Great I can eat it, wait it was negative question, so yes means no.
  • "No, I want it" OK now, I am confused, no means yes, which means I can eat it, but "I want it" implies that she wants to eat it. This answer contradicts itself!
  • "Yes, you can have it." Great another contradiction, yes, she minds, and yes, I can have it.

But this blog is about coding, so lets get back to the world where ambiguity does not exist...

Recently I came across this piece of code (I wish I was making this up) which took the negative question to a new level of confusing.

if ( ! information_hiding_disabled) {
...
}

I don't know about you, but I personally have to think really hard until I can figure out under which conditions this executes. Even knowing what the the value of the flag is, makes it hard to think about what it represents. The issue here is that "information hiding" refers to a negative concept. Adding the word "disabled" to it adds a negation to the concept which I guess makes positive again. Often times, when you actually want to use it in an "if" statement you want the opposite behavior and hence you may need to add "!/NOT" So now you essentially have this:

if ( ! ! ! show_information) {
...
}

Lets look at a loop as a good way to name a flag:

boolean done = false;
while(!done) {
if (...) done = true;
}

Now lets look at the opposite and notice that it is harder to read:

boolean notDone = true;
while(notDone) {
if (...) notDone = false;
}

In the second loop the "while(notDone)" is easily readable, but the issue are the assignments, especially the exit assignment "notDone = false" Here again, double negation is going on and it takes my brain just a little longer to figure out what "false" means in this case. Furthermore there may be an "if" statement which would make it worse.

if ( ! notDone)

So in conclusion don't ask negative questions! Variables/flags which are named with positive concepts are much easier to read than variables which represent negative concepts. Please think about this when you write your code. Otherwise, I get a headache.