views:

29

answers:

1

Very often when you code you have a flag indicating two possibilities, like this:

CACHING_AUTHORIZED
CACHING_UNAUTHORIZED

or

CACHING_ON
CACHING_OFF

If you then use a boolean to store this, very often it just "reads weird":

CACHING_ON = false;
CACHING_OFF = true;

or

CACHING_ON = true;
CACHING_OFF = false;

One of the possibility is to write:

CACHING

And then simply code using:

if ( CACHING )

or

if ( !CACHING )

but sometimes you really do want both the 'ON' and 'OFF' forms.

The thing is: it doesn't change much which one you pick, it simply doesn't make much sense, no matter how you turn it.

It is not entirely dissimilar to the "getter" problem for boolean, which is certainly subject to a lot of heated debate (should we use isConditionTrue or getIsCondition or isCondition or getCondition or simply condition or conditionTrue etc.: I'm being a bit facetious but the point stands and there have been very long debates on this and the topic certainly ain't close).

My question is different: seen that neither

CACHING_ON = false;
CACHING_OFF = true;

nor

CACHING_ON = true;
CACHING_OFF = false;

make sense, shouldn't we simply get rid of the boolean and do:

CACHING_ON = 1;
CACHING_OFF = 2;

Sure, you're losing the boolean but at least '1' and '2' cannot bring any confusion.

My point is: using a boolean and true/false is very often confusing because we're mixing two different things: english and logic and they simply, well, don't mix at all.

Is it bad to use an int here even tough there are only two possibilites?

+1  A: 

Your method makes it unclear the value is a boolean. I feel like I should be able to assign the value 3.

I don't really get your point. If you have 2 booleans you're representing 4 possible states so you shouldn't really be naming the variables in a way that suggests there are only 2 options. Maybe you should be using an enum and constants or flags instead.

CACHING = ON
CACHING = OFF
CACHING = EITHER
CACHING = UNKNOWN
CACHING = UNKNOWABLE
CACHING = UNTHINKABLE
SpliFF