This section is another little vacation about cutesy language features. It’s fine to skim or skip. The main reason to know them is when you see them in other people’s code.
You can use these tricks, but they’re sort of like +=. They don’t do anything you couldn’t do before.
A trick we’d like to borrow from math is using letters for constants. For example, we might want to make cpi a shortcut for 2.54 (centimeters per inch.)
We could just write it like this:
But the program might be easier to read, and a little safer, if we had a rule to
lock cpi to that value, and make it an official constant. We do. The rule isn’t a huge
improvement, but it’s short and easy to remember.
When you declare a variable, putting const in front locks it to that value. You have to use the declare-and-= trick when you do this. Examples:
You don’t have to use them only for “real” things. You’re allowed to use the rule for anything:
I can’t think of any possible reason anyone would want to have abc as a locked-in
shortcut for 12, but it’s legal.
You use them like regular variables, except you can’t change them (trying to is an error):
The advantages are minor. They look a little different in the pop-up (mine have
an orange F next to them) and we can’t accidentally change them. The main
advantage is making the program a little easier to read.
You can declare const’s locally. This code checks whether we’re off the sides, with an extra 0.02 unit tolerance:
Using all caps and underscores is an older style for writing constants.
Constants have one other small, tiny advantage – they run just a tad faster. The compiler replaces them with the numbers. When your program runs, there’s no variable look-up needed.
Sometimes we use int’s to stand for an option, in a clunky sort of way. For example, we might make a variable holding different types of cars, set up like this:
Using numbers to stand for things is a solid computer idea, but that last line is icky.
An enumerated type is very simple way to make this easier to read. Here’s a rewrite. The first line describes a car-type to the computer:
That says we can declare CarT variables, which are really ints going from 0 to 3. The computer automatically numbers the words. sedan is 0, compact is 1, convertible is 2 and van is 3.
enum is a keyword. It’s only used in this one place.
Here’s a rewrite using our new CarT:
This is exactly the same as the previous version. It declares car1type as an int, assigns 3 to it, and compares it to 1. But it looks so much nicer.
If you type out the second line, CarT-dot even gives you a pop-up with sedan
through van. It’s reading them from the first enum line we wrote.
To show it in use, this function tells you how many doors your car has. This is the “old” version, using ints and the hand-made table:
This version is exactly the same, using the exact same numbers, but using the CarT enum:
Notice how it takes a CarT input. That’s a much nicer hint about how it works,
even if it’s still just an int.
Another, small, enumerated type example that doesn’t really do anything:
Here’s a real enumerated example from Unity. You can spin a phone four different ways. 1-4, plus 0 for unknown. Unity uses an enum to label them:
That tell us DeviceOrientation is another word for int, and it has 5 possible
values, with those names.
Unity creates one global of that type, Input.deviceOrientation, and auto-sets it as you tilt the tablet. Notice it’s in the Input namespace.
Sample code using it:
This is really checking whether d is 1 or 2. But it’s easier to understand with
One more example, from standard C#. You can open a file for reading only, writing only, or both. A 0-2 int says what you want. In the old days, you wrote open("cow.txt", 0);. The second input was how to open it, and everyone knows 0=read only.
C# added a 0-2 enum:
Now we can use the nicer-looking open("cow.txt", FileAccess.Read);. It’s extra nice because if we didn’t know how the command worked, we’d see the second input is a FileAccess variable. Typing FileAccess-dot shows the options.
What makes ifs useful is you can put any commands, and all you want, into each part, and you don’t even have to have an else. But there’s one common way to use an if that could use a short-cut:
In our minds, the if is really picking between "big" and small". We’re thinking of it more like this:
We decided to make a special type of if that works like that. The true and false parts are naked values, of the same type. It returns one of them. Legal code for what we were trying to do:
Everything after the = is the special if. The rules are:
You can use the int-instead-of-float shortcut, like in float f = n<10?4:3.5f.
Putting it inside string math is semi-common. Two more examples of that:
num==1?"":"s" is sneaky. We only want to print an extra "s" for plural, but we
have to give some answer otherwise, so we use "".
Some general examples:
Some people use this trick for “n can’t be less than 0.” This looks funny but it works:
It can also be nested, but when they start getting that long, it might be better to rewrite them as real ifs. This figures out “ice”, “water” or “steam” based on temperature, using the same logic as a cascading if:
The computer will sometimes allow you to mix enums and ints. They’re the same
thing, but not every combination is allowed.
car1type=3; is an error. Even though 3 is Van, it wants you to write out
You almost never want < and > to compare enums, but you can. It compares the int values:
Likewise you almost never want to add, but you can. car1Type++; is legal. It goes
to the next car in the list. Past Van it will go to 4, which isn’t a car. That’s odd, and
makes no sense, but legal.
The numbering normally starts at zero. We usually don’t care about the exact numbers, so 0 is fine. But, if you want, you can pick the numbers using = after an item. Items without = go up by one. Examples:
This is a pretty obscure rule, and there’s rarely a reason for it. But I like how it shows how enums really are just int’s.