If you understand booleans in C, then you know that you don't really understand booleans in C. That's the case with Bodo's co-worker, anyway. They either don't understand booleans, or they don't understand their business requirements.

switch (getBool()) {
case false:
  /* do something... */
case true:
  /* do something else... */

Now, at first glance, this looks like a really badly written if. getBool returns a boolean value, and then we switch on that value. A boolean can only have two values… right?

Well, in C, the bool type is just a wrapper around an integer- usually a byte- with a few helper macros. Whether you're using stdbool (available since C99), or some homebrew version is a bit of a mess, but the important thing is that bools may hold any integer.

Now, all that said, it's still definitely a code smell to see this swifch, it's just not quite as dumb as it looks. In fact, it's dumber.

This code exists in a safety critical industrial system. And that system has a rule: any unexpected values means the system should perform a full stop immediately. After all, if things are at the point where a function which returns either a 0 or a 1 starts returning something else, you can't trust the program anymore. Stop and wait for an operator to recover it.

So that default, arguably, should be there. It should also do something.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!