7

I've been thrown a bit of a poser, and I'm not sure what the answer is.

Basically - is there a convention for what values to use in tristate data-types? Doing some googling around, it doesn't look like there is: I've seen:

  • -1 = False, 0 = Not known/undefined, +1 = True
  • 0 = False, +1 = True, +2 = Not known/undefined
  • -1 =Not known/undefined, 0 = False, +1 = True

..amongst others. I'd rather use a well-known convention if there is one. Otherwise I'll make one up :-) It may well be there is no right answer, but just thought I'd dig a bit deeper...

Edit
Found this one as well that Microsoft seem to use in recent code: -1 = true, 0 = false, 2 = not known. I assume having 2 == unknown means it removes the ambiguity over interpreting +1/-1 when just looking at the raw values in a debugger/dump/memory etc. Oddly enough, this option appeals just for this reason alone (removes chance of forgetting which variation of 1 means 'true').

Chris J
  • 30,688
  • 6
  • 69
  • 111
  • 1
    The middle one looks interresting; a tristate where two of the states are the same... ;) – Guffa Jun 07 '11 at 11:13
  • @Guffa - not for me. I very often works with Windows API and they in most cases uses 0 as False state. That's why I like the third case where 0 = False. –  Jun 07 '11 at 11:51
  • @daemon_x: I don't think that you read my comment thoroughly... – Guffa Jun 07 '11 at 12:03
  • @Guffa -- copy & paste fail. Fixed, ta :-) – Chris J Jun 07 '11 at 12:13
  • I always wanted a tristate bool. Fed up of having to create enums for something i encounter many times. Wondering if any language offered a tristate thing by default. 3rd one is cool though here.. – nawfal Sep 17 '11 at 07:56

4 Answers4

3

To my knowledge, there is no convention for that.

There isn't even a single convention for a boolean value, the most common are:

  • 0 = False, -1 = True
  • 0 = False, 1 = True

Another common (but not universal) convention that would be relevant is the usage of -1 for undefined values.

If you choose to use -1 for undefined, you could use 0/1 for False/True:

  • -1 = Undefined
  • 0 = False
  • 1 = True

And naturally I have to mention that the third state should of course not be undefined/unknown, but FileNotFound. ;)

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • The Unix shells also use 0=True, anything else = False. That's partly why you write `return 0;` in C; because a return value of 0 means everything was fine and anything else means something was bad. – ShreevatsaR Jun 07 '11 at 11:52
  • 1
    I'm used to boolean being not quite universal, but that 0 is false *is* generally universal. Thus it's usually safe to assume that non-zero is true (regardless of implementation). Shells are a red herring, they aren't stictly boolean; the return value is the error code, with 0 generally meaning "no error", and any other value being indicitive of what the error actually was. – Chris J Jun 07 '11 at 12:16
  • +1 simply for the classic^H^H^H^H^H clbuttic daily WTF (I was going to mention it in comments) – Gerry Coll Jun 10 '11 at 03:33
3

Regardless of the semantic meanings you attach, I would choose your three values as one negative number, one positive number, and zero. This allows optimal testing for membership in each possible 1- or 2-element subset of the 3 possible values with just a single test and branch (>0, <0, ==0, >=0, <=0, !=0).

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
1

I would say there's nothing like convention, but I would prefer the third case you've mentioned because I've seen this in use many times.

Value       State
----------  ----------
 -1         Undefined
  0         False
 +1         True

I wouldn't prefer the first two cases because pure boolean states are mostly determined as 0 = False and 0 <> True so it might be little confusing.

0

If you're looking for a substitute for a tri-state bool with int, you can always have the nullable boolean without having to worry about standards. Something like, in C# bool? triState. It can have values true, false or null which can be considered as unknown - but I see that's not what you're looking for.

I don't know about convention. Most certainly not from the answers given here. I like your third one.

But a different perspective, my pick would be:

{ True, Unknown, False } => 0, 1, 2 // going by enum like values...

EDIT: After a good point by @LarsTech, I should clarify. That if I know there can be more than two values for a variable then I would think like as its an enum and hence I could translate it to { 0, 1, 2 }. I always follow a pattern for enum values like this : "Good, Alright, Poor". It comes naturally to me, but for many true having a "0" is not welcome. One can always have a reverse order.

nawfal
  • 70,104
  • 56
  • 326
  • 368
  • 1
    Your "pick" would make True = 0, something the rest of the world has decided should be false. – LarsTech Dec 28 '11 at 02:41
  • @LarsTech Accepted True=0 is not conventional, but it still finds a place in languages like Ruby and Lua and in bash scripts. The accepted answer on this thread states it too. I need not mention how C interpreter takes it internally to denote success (though doesnt explicitly translate to true). Something similar, I remember learning in college about digital signal encoding techniques where 0 denotes high voltage. – nawfal Dec 28 '11 at 08:18
  • @LarsTech But coming to my point, it would be confusin for me too had it been a case of just true & false and assignin them 0 & 1 respectively. But here the OP is askin for a 3 state convention i'm comfortable with the my pick if I can think from an enum perspective. Enums does the best job of handlin more than 2 state requirements & I would say one should think like its an enum & hence "0,1,2" is a good choice. May be its just me that who loves a convention in enums like: Very good, good, quiteOk, doesntMatter, bad, pitfall. I usually go for this. Since there isnt one convention, its my idea. – nawfal Dec 28 '11 at 08:23
  • "*True=0 is not conventional, but it still finds a place in [...] in bash scripts*" -- Not quite. The program exit code 0 means the absence of any error (= false) where any nonzero value indicates some kind of an error (true) – JensG Jan 11 '22 at 15:14