Some people write code that is incredibly bad. It is a constant source of amazement to me that they manage to perform basic tasks such as crossing a road without coming to a sticky end. The fact that they can find a manager stupid enough to give them a high salary as a programmer is even more unbelievable.

Have a look at this code snippet:

#define FOO 1012

...

char *buf = some_func(); // returns a string representation of a number
if(strncmp(atoi(buf), FOO, 4))
OK. That code compiled with no errors because it was a pre-ANSI C compiler (an old IBM AIX C compiler that will happily accept either pre or post ANSI C unless you tell it otherwise). So any function which lacked a prototype would default to taking all int parameters and returning int. The "#include <string.h>" had been intentionally removed from the source file (I checked in Source Safe).
What had happened is that the previous code had the following:
if(strcmp(buf, "1012"))
Compare it to what I would have done:
const char * const FOO = "1012";
// The following works almost as well.
// #define FOO "1012"

if(strcmp(buf, FOO))
The original version would have worked just as well as what I would have written, except that my version has better type checking and would use less memory in some situations (if FOO is used in multiple places). But the original code was quite OK. The problem began when the programmer decided that he should use a macro for the "1012" instead of an inline string literal. He apparently didn't know that you have to put quotes around a string literal when in a macro.
So when he got compile errors related to passing an int instead of a string he removed the string.h header file which fixed that error. Then of course he got a compile error on the other parameter. So he used atoi() to convert it to an int too.

I pointed this error out to him in the most polite way possible, most people who know me would not have believed me capable of being as polite and subtle as I was on that occasion. However the project manager didn't think that this was appropriate. I was not supposed to find any bugs in other people's code, and I wasn't supposed to distract them from their work by informing them of such bugs. The project manager threatened to sack me if I did this again, so I resigned immidiately.
Fortunately the project didn't involve any software that was at all critical (not air-traffic control or a military system) so I can sleep easy at night knowing that I'm not going to suffer in any way from the bugs I wasn't allowed to fix.


My email address is at the bottom of the page. If you know of code that's worse than this then please do a suitable write-up and send it to me. Don't send me the actual bad code (that is copyright by the company who paid for it), send me something similar that you've written while in a situation where you couldn't see the code (EG after walking out of a site because they wanted you to write bad code).
Don't submit code that you've heard about. Don't submit PD code or code from a text book (some of them have some very bad examples of code). Only submit code that you personally have worked on in a professional capacity. The code must also be from (or destined to go into) a production system.
When you submit such code fragments to me please make it clear whether you would like to see it published on my web page (I am prepared to publish anonymously if requested) or whether you're just sending it to me for my own amusement.
If you provide me with anything that I put on my web page then I'll buy you a drink if we're ever in the same city!
Copyright © 1999 Russell Coker, may be distributed freely.