Stupid and useless

2017 May 15

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:

  1. It violates common idioms on C string usage, like an array initialised by a single string
  2. It creates unrealistic expectations from the rest of the code, particularly by standard library functions
  3. It imposes unnecessary complexity for whoever wants to access the beginning of "in" or "out"

Useless

The gem is useless because:

  1. 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 to char
  2. It leads to code like &directions[flag == dir ? 0 : 3] (actual code) everytime someone wants to access "out"
  3. It does not save memory, and it’s not faster than any other alternative

I hope this has been instructive. ‘til next time!

« Past

Tagged : stupid, useless


Comments:

comments powered by Disqus