Magic Code
Okay, so I absolutely hate Magic Code. That is, to say, code that either fixes or breaks a project, module, etc. with no real reason. For instance:
//fLog is a FILE opened elsewhere using a call to fopen
if(fLog)
{
DoStuff();
}
else
{
printf("%i\n",GetLastError());
} The code above is a somewhat redacted version something I was working on the other day. As it’s displayed there, DoStuff() will execute. This means that fLog opened fine. If, on the other hand, I comment out the printf statement in the else case, DoStuff() fails to execute, because fLog doesn’t open. In short: commenting out a debugging printf in an else case causes the call to fopen to fail. Why? I have NO IDEA. As a result the following line now mars my code:
printf("%i\n", GetLastError()); //Magic code
And I it drives me insane. The module’s working. It’s been unit tested and it works perfectly every time. Unless I remove one little printf statement. A printf statement which never fires. A printf which falls in an else block after the line which it “fixes”. A printf which shouldn’t have, nay, CAN’T have any effect whatsoever on the line in question. And yet if I remove said printf, the call to fopen fails and the whole module breaks. Inexplicable. Infuriating. Magic code.
July 6th, 2008 at 1:07 am
That’s just creepy.
July 6th, 2008 at 9:41 am
I hate it when things like that happen. The other day I added a line of code to my upload page, and when I tested the change, it had broken the page. So I removed it. And it was still broken. Very confusing.
July 7th, 2008 at 2:31 pm
Also speaks English and Spanish.
July 10th, 2008 at 9:42 am
Chris: I’ve had things like that happen. Once in my Data Communications class, I had a program that just WASN’T working. It was compiling but breaking inexplicably. I traced down the exact line the problem was occuring on and, after struggling with it for several hours, I reverted it back to the original line that I’d had there before.
It magically started working.
September 3rd, 2008 at 1:13 am
[…] Magic Code […]
October 28th, 2008 at 8:46 am
Well written article.
November 26th, 2008 at 4:07 am
Hello webmaster I like your post “Magic Code” so well that I like to ask you whether I should translate into German and linking back. Greetings Engel