Brushing teeth and static checks
The three-year-old at home doesn't like having his teeth brushed. I don't blame him. It's hard at that age to see why someone would get to mess around with a brush in your mouth, when you'd rather be reading a bedtime story, or play with your toys.
But I, a crafty father, have developed a trick. Experienced parents will recognize this as a kind of distraction tactic.
<masak> Noa, I'm going to paint inside your mouth.
<noa> Red, and white, and blue, and pillow.
What he has specified here is a list of colors (different each morning or night), but the last item is not a color. I start brushing, and slowly list the colors he has picked:
* masak brushes noa's teeth
<masak> Red... white... blue...
<masak> ...pillow... HEY, WAIT A MINUTE!
* noa laughs, toothbrush in mouth
Voilà, a boring chore has an element of silly in it, and is much easier for both parent and child.
In programming terms, the item "pillow" doesn't belong in a list of colors. (It doesn't typecheck.) My scripted mock surprise at this makes the three-year-old feel like he has somehow outsmarted the tooth-brushing routine — instead of being an involuntary recipient, he is an active participant (and a mischievous one at that). He's a tooth-brushing renegade.
Some programming languages are permissive like that: they accept any (syntactically correct) program, start running it, and only later exclaim "HEY, WAIT A MINUTE!" when your program runs into some fairly obvious trap or other. The programmer's reaction is usually "oh right, shucks, better fix that", possibly after some thinking and tracing around the code.
Other languages are more strict, and disallow many such mistakes at compile-time (or earlier). The checks are often type checks, but could also be other things, such as reachability checks.
There are long-winded discussions and arguments on either side about which type of language is better suited for various tasks. Briefly and a little bit inaccurately, those can be summarized by weighing flexibility/freedom on the one hand against correctness and safety guarantees on the other.
But my first thought was Most or all of these problems would be caught by TypeScript. At least if you use TypeScript with enough strictness turned on (such as the options
strictNullChecks), and provided you don't cheat and manually type variables as
My second thought was Why do we do this to ourselves?
So, yeah, go check out TypeScript if you haven't already. You might like it. TypeScript's designer Anders Hejlsberg (of C# and Delphi fame) once described TypeScript as a value proposition it's not rational to refuse. It has only benefits, and no drawbacks. That about sums it up for me.
null was a billion-dollar mistake, here's a wild idea but maybe we should try using a language without them?)