views:

95

answers:

9
+2  Q: 

Naming booleans

If I only want to check if something is impossible or not (i.e., I will not be using something like if(possible)), should I name the boolean notPossible and use if(notPossible) or should I name it possible and use if(!possible) instead?

And just to be sure, if I also have to check for whether it is possible, I would name the boolean possible and use if(possible) along with else, right?

+4  A: 

You should probably use isPossible.

Negative names for booleans like notPossible is a very bad idea. You might end up having to write things like if (!notPossible) which makes the code difficult to read. Don't do that.

Mark Byers
Will
...or `useCurrentTariff` or `hasSurcharge` as applicable
Rowland Shaw
+1. I always try to keep everything in the "positive" even when I'm more interested in the negative. Another example would be using `is_open` instead of `is_closed`... mostly just for consistency in that case, I guess.
Mark
A: 

Whichever is easier to read in your specifc application. Just be sure you don't end up with "if (!notPossible)".

Ray
+4  A: 

I tend to err on the side of positivity here and use possible. It means someone can't write some code later that does this...

if (!notPossible)

Which is unreadable.

Jason Punyon
Two negatives... that means it's *totally* impossible right?
Mark
Or you could go one step further and write: `if (!notImpossible)`
Mark Byers
Or if we use Alex's lingo `if (!lacksImpossibility)`
Jason Punyon
Or a real mind bender: if (!maybePossible)
Bob
A: 

I think it's considered preferable to avoid using negatives in variable names so that you avoid the double negative of if(!notPossible).

Adam Pope
A: 

I recommend using isPossible. It just seems to make a lot of sense to use 'is' (or perhaps 'has') whenever you can when naming boolean variables. This is logical because you want to find out if something is possible, right?

Matt H
What if you want to find out if something is _impossible_?
wrongusername
You can always put a `!` on a `isPossible`, but you can't take the `not` off of `notPossible`.
tloflin
+1  A: 

I like to name booleans with consistent short-verb prefixes such as is or has, and I'd find a not prefix peculiar and tricky to mentally "parse" (so, I suspect, would many readers of the code, whether aware of feeling that way or not;-) -- so, I'd either name the variable isPossible (and use !isPossible), or just name the variable isImpossible (many adjectives have handy antonyms like this, and for a prefix has you could use a prefix lacks to make an antonym of the whole thing;-).

Alex Martelli
A: 

I agree that negatively named booleans are a bad idea, but sometimes it's possible to reframe the condition in a way that's positive. For example, you might use pathIsBlocked rather than cannotProceed, or rather than isNotAbleToDie you could use isImmortal.

Chuck
+1  A: 

I generally try to name my flags so that they will read as nicely as possible where they are being used. That means try to name them so that they won't have to be negated where they are used.

I know some folks insist all names be positive, so that people don't get confused between the negations in the name and those in their head. That's probably a good policy for a boolean in a class interface. But if its local to a single source file, and I know all the calls will negate it, I'd much rather see if (impossible && ...) than if (!isPossible && ...).

T.E.D.
A: 

You should name it for exactly what is being stored in it. If you're storing whether it's possible then name it isPossible. If you're storing whether it's impossible name it isImpossible.

In either case you can use an else if you need to check for both cases.

From your description it seems more important to you to check for impossibility so I'd go with isImpossible:

if(isImpossible)
{
    // ...
}
else
{
    //...
}
Bob