Skip to content


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.

Posted in Geekery.


8 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Andrew says

    That’s just creepy.

  2. Chris Sullins says

    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.

  3. Citizen Jane says

    Also speaks English and Spanish.

  4. The Tarquin says

    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.

  5. Zea says

    Well written article.

  6. www.engel-bedeutung.de says

    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

  7. webkatalog says

    Good informations, keep up the good work.

Continuing the Discussion

  1. More Money To Spend Blog Presents New Breakthrough "Magic Code" Makes Your Site Money! linked to this post on 2008.09.03

    […] Magic Code […]