Welcome, neighbours, to a new series of posts. These posts will illustrate bits and pieces of code which is both stupid and useless. In other words, code which should be almost extinct, but for reasons contrary to conscientious engineering practice, it survives.
Let’s dive in to our first gem!
The gem
static const char directions[] = "in\0out";
Stupid
The gem is stupid because:
- It violates common idioms on C string usage, like an array initialised by a single string
- It creates unrealistic expectations from the rest of the code, particularly by standard library functions
- It imposes unnecessary complexity for whoever wants to access the beginning of
"in"
or"out"
Useless
The gem is useless because:
- The same thing can be done in a more readable and idiomatic way by using an array of either 2
arrays of
char
or 2 pointers tochar
- It leads to code like
&directions[flag == dir ? 0 : 3]
(actual code) everytime someone wants to access"out"
- It does not save memory, and it’s not faster than any other alternative
I hope this has been instructive. ‘til next time!
« Past Future »