Tuesday, August 31, 2010

The antipattern of hate

Code changed to protect the guilty, but the approach is the same. Also, this pseudo-code below is actually more streamlined, more sensible and slightly saner than the original.


static char* array[10];
static int other_array[10];

void get_values(int n)
{
for (i = 0; i < n; i++)
other_array[i] = get_val(array[i]);
}

int main (void) {
array[0] = "somestring";
array[1] = "some other string"
array[2] = "lalala"
array[3] = "bananas"

get_values(4);

for (i = 0; i < 4; i++)
blah(other_array[i]);

return 0;
}


If you think of writing code like that, don't. Google for function parameters and try to understand how they work. Some poor sod will eventually have to figure out wtf you're trying to do [1]. I've encountered this bit of code a few weeks back and I am still flabbergasted.

/me hopes this venting makes the anger go away.


[1] as I stated in an early post, any software project is a collaborative project. It has at least two developers [...]. The person you may end up hating for the above may be you.

3 comments:

Unknown said...

As embedded developer, I've found a lot of this evil source code in various software, internal or external. Sadly, most is internal code ( sprintf instead of snprintf, ... ).

For external, freeradius-client is a good example (config option missing ? crash), and vlc 0.8.6 with every single malloc checked (for 1 byte or more) and in a file, hardcoded malloc with 1 MB without any check !

Anonymous said...

I'm *still* trying to figure out what that code does. Or rather, it seems clear *what* the code does, but *why*?

Peter Hutterer said...

Anon:
this simplified example obscures the original purpose of the code but it basically needs to get some settings from an external source and process them in some manner (function "blah()" in the example).