# True/False vs 0/1

The first and last thing I’d say to anyone picking up CS is do what works for you. This is a coding language originally designed to be used as widely as possible, by authors at all ranges of coding skill, not one designed to be as efficient or elegant as possible. The time you spend pushing out of your coding comfort zone to learn new things can be worthwhile, or it can just be time not spent finishing your actual game, slowing down your writing significantly as you try to incorporate things you’re not comfortable with. (Or even worse, spending time going back over existing working code and condensing it into a more efficient or elegant form. I promise you, it’s WAY more important to finish your game than to satisfy the aesthetic preferences of other hypothetical code-readers.)

With that said, if you’ve got a variable that you know will only be able to be set as true or false, there are a couple of minor reasons you might want to create it as a Boolean rather than numeric.

First, you can take advantage of the simplicity of Booleans to save a bit of typing. Instead of:

``````*if met_jones = 1
"Hello again, Jones," you call.
*if met_jones = 0
You introduce yourself to Jones for the first time.
``````

you could have:

``````*if met_jones
"Hello again, Jones," you call.
*if not(met_jones)
You introduce yourself to Jones for the first time.
``````

For a Boolean, you don’t need to type out the true/false, like you do for the 0/1 of a numeric variable. If the variable is true, you don’t need to type anything else, which is nice. Probably also makes it a little easier to spot typos or errors? That’s so subjective that I hesitate to hang too much on it, but it’s true for me.

Similarly, if you’re ready to try multireplace, it’s that little bit simpler with a Boolean variable. If your variable is numeric with possible values of 0 and 1, you could use multireplace a couple of ways:

``````@{(met_jones = 0) Introduce yourself to Jones.| "Oh hi, Jones."}

@{(met_jones+1) Introduce yourself to Jones.| "Oh hi, Jones."}
``````

(These work for different reasons. The first version is testing the statement “(met_jones = 0)” as a Boolean… if it’s true that met_jones is zero, the first text snippet will be displayed, and if met_jones has any other value, you’ll get the second. The second version is testing `met_jones` as a numeric variable… but because numeric multireplace’s first text snippet is always for a value of 1, not 0, you need to add one to `met_jones` for it to work.)

Compare either of those to the Boolean multireplace:

``````@{met_jones "Oh hi, Jones."|Introduce yourself to Jones.}
``````

I don’t know about you, but less hunt-and-pecking for the = and () signs is a small but meaningful quality-of-life difference for me when I’m writing.

Before I started using multireplace more, I would also regularly `*create` a variable as 0, even if all the values checked in the game were in a short nonzero range. Because multireplace starts counting at 1, but must include an option for every value that’s actually reachable in the game, that had the annoying effect that I would need to test for (var+1) rather than var, and I’d often have no meaningful value for the first space:

``````Your alibi is @{(alibi+1) Nothing, this is alibi=0|Joe|Bubba|Mahershala}.
``````

Now I much more often create the variable as 1, if I know it will only ever have a set number of positive values. That lets me just write, e.g.

``````Your forged name tag says "@{alibi Joe|Bubba|Mahershala}."
``````

More on multireplace here. I honestly never thought I’d use it, but since I dove in a couple of years ago, it’s made it a lot quicker and tidier to code variations into my text. Especially in combination with macros, so I don’t need to hunt for @{ | }.

And with all that said: Do what works for you.

2 Likes