2015/09/06

Bools in C

I like the bool type in C.  Finally added to C99, and pretty much acceptable in all decent C using circles now. It's fantastic to be able to document that something is true or false, instead of just being 1 or 0, or one of dozens of stupid enums.

However it's often not a good choice for function parameters.

For the basic case of a single argument function it's great:
nashlib_autofree_pointers(true);
A reader knows exactly what is going on there.

However consider the case of a function which takes more than a single argument:
nashlib_autofree(NL_AUTORFREE_TYPE_POINTERS, true);
Okay, so we can probably guess what is going on... but what about when we start getting three or four arguments:
nashlib_config(NL_AUTOFREE, NL_POINTERS, true);
And boom, suddenly maintainability is a problem.

 Short version: Avoid bools for parameters. Use them for returns however.

No comments: