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.

11 Responses to "How not to name a boolean variable"

  • Dan R says:
  • tsk tsk says:
  • Neil says:
  • amrelgarhy says:
  • John Doe says:
  • Amit says:
  • Amit says:
  • Amit says:
  • Chase saunders says:
  • Amit says:
Leave a Comment