Today I saw the following (horrible) code (variables names have been changed to protect the innocents):
char *aux; aux = NULL; /* workaround for compiler warning */ if (value == 1) aux = “something”; else if (value == 2) aux = “something”; else if (value == 3) aux = “other thing”; if (strcmp (aux, “something”) printf (“I said something!”);
One cookie for anyone that sees two stupid things. Hm, no, it’s too easy, no cookies.
First, you have that stupid comment saying “workaround”. What workaround? If value is not 1, 2 or 3, aux isn’t initialized and you get a nice compiler warning. YOU FORGOT THE OTHER OPTIONS!
Next, you have two if’s doing the same thing (I just did an attribution, but the original code did a lot more). WHY THE HELL ARE YOU REPEATING CODE? MORE LINES == MORE BUGS!
Man, there are some people around that doesn’t get what a “code path” means. I mean, I could easily spot the problems just on the first look. This look like a code written by someone that never ever did a “table test” (you know, you write your code on a paper and test it with numbers you made up, checking what would happen on each line).