• Tino (unregistered)

    // love CPP #define true !toggler #define false toggler #define void static int toggler; void #define ToggleFilter(X) ToggleFilter() { toggler=!toggler; }
    struct no_error_here__dude

    // Spoiler: This fix is untested

  • (cs) in reply to Niels
    Niels:
    Yeah, the #define in c/c++ are actually pre-compile time text processing. So this code will look something like this the moment it is actually offered fed into the compiler:

    void CReport::ToggleFilter() { }

    bool CReport::IsFilterEnabled() { return true; }

    <NITPICK>Actually it always returns false.</NITPICK>

  • (cs) in reply to Matt
    Matt:
    I can understand someone making this mistake, but why didn't that person test what they wrote? Why didn't Q&A catch it either?

    You assume there was a Q&A department to catch it in the first place. You also assume the shop uses tests.

  • Colonel Angus (unregistered)

    I don't see the problem... the code should work just fine!

  • Niels (unregistered) in reply to dpm
    dpm:
    Niels:
    Yeah, the #define in c/c++ are actually pre-compile time text processing. So this code will look something like this the moment it is actually offered fed into the compiler:

    void CReport::ToggleFilter() { }

    bool CReport::IsFilterEnabled() { return true; }

    <NITPICK>Actually it always returns false.</NITPICK>

    Yep, and this proves that this forum has more QA than the company where the original code was written ;)

  • OhDear (unregistered)

    I suspect the unit tests were all done using compiler directives and thus passed with flying colours.

  • Bri (unregistered) in reply to abx

    My Eyes! - Nice one abx. You made me laugh.

  • (cs) in reply to java ignorant
    java ignorant:
    so somebody thought #define would work at runtime? (Sorry I am a java idiot!)

    Pretty much.

  • FIA (unregistered) in reply to cthulhu
    [image] There, FTFY.
  • Marco (unregistered)

    I ... don't understand...

  • jethr0 (unregistered)

    Somebody obviously had a deep insight into the difference of compile time and runtime ;)

  • tt (unregistered)

    strange.. I always thought that toggle function would return one of 3: true, false, fileNotFound... Here the hell is fileNotFound!?

  • The Sussman (unregistered) in reply to cthulhu
    #define ENABLE_BRILLIANT
    void CReport::ToggleBrilliant()
    {
    #if defined(ENABLE_BRILLIANT)
       #undef ENABLE_BRILLIANT
    #else
       #define ENABLE_BRILLIANT
    #endif
    }
    
    string CReport::IsBrilliantEnabled()
    {
    #if defined(ENABLE_BRILLIANT)
         return string("Brilliant!");
    #else
         return string("False");
    #endif
    }
  • tecxx (unregistered)

    excellent wtf =)

  • Fast Eddie (unregistered) in reply to The Sussman
    The Sussman:
    #define ENABLE_BRILLIANT
    void CReport::ToggleBrilliant()
    {
    #if defined(ENABLE_BRILLIANT)
       #undef ENABLE_BRILLIANT
    #else
       #define ENABLE_BRILLIANT
    #endif
    }
    

    string CReport::IsBrilliantEnabled() { #if defined(ENABLE_BRILLIANT) return string("Brilliant!"); #else return string("False"); #endif }

    "The Sussman" is teh brillant. He am be teh n00b?

  • Nox (unregistered)

    The more I look at this, the more stupid it seems. I was going to write stupider but I figured people might not see the irony.

    It is stupider though.

  • Sean (unregistered) in reply to Vechni
    Vechni:
    What is so LOLOLOLOL EPIC WTF about a programmer fluent in ____ language writing C++ ? This is pretty common/mundane. Does the level of epicness derive from this being a SOD that most here actually comprehend, because I think that's exactly the kind of shenanigans that are going on.

    If you don't know C well enough to know that #define isn't handled at execution time you shouldn't be writing production applications

  • the real wtf (unregistered) in reply to Sean
    Sean:
    Vechni:
    What is so LOLOLOLOL EPIC WTF about a programmer fluent in ____ language writing C++ ? This is pretty common/mundane. Does the level of epicness derive from this being a SOD that most here actually comprehend, because I think that's exactly the kind of shenanigans that are going on.

    If you don't know C well enough to know that #define isn't handled at execution time you shouldn't be writing production applications

    Thats basically every microsoft programmer I've ever seen.

  • eric76 (unregistered) in reply to ath
    ath:
    That's not a WTF. Using the pre-compiler for calculations is a common technique in libraries such as boost.
    My personal very strange use of a precompiler was to recursively compile a program.

    It was for a graduate parallel processing course I was taking. The program used the same algorithm to do some matrix work (I think I was inverting matrices) using a number of different variations in how much data was passsed to each processor and how much it did before passing it to another processor or processors.

    The code was mostly identical with some changes depending on the details of each set of parameters.

    So when compiling the program, there were a number of #ifs to determine the depth of nested compilations, #undefines to undefine the previous parameters and #defines to define new paramaters. And then, after the code was compiled, except for the final path, it would #include itself for the next pass.

    The program worked great, but it was one of the most miserable things I ever debugged.

  • Neeneko (unregistered)

    Looks like someone was coming in from an interpreted language and didn't realize that C/C++ were compiled. (or even worse, for some reason they were used to using an interpreted version of C!)

  • Jughead (unregistered) in reply to Neeneko
    Neeneko:
    Looks like someone was coming in from an interpreted language and didn't realize that C/C++ were compiled. (or even worse, for some reason they were used to using an interpreted version of C!)

    Pain in the arse how you have to press a button, or run a makefile or use a command simiar to cc to ghet these damn interpreted programs to work these days....

    (and has anyone mentione brillant over brilliant yet....)

  • Drande (unregistered) in reply to ObiWayneKenobi
    ObiWayneKenobi:
    Matt:
    I can understand someone making this mistake, but why didn't that person test what they wrote? Why didn't Q&A catch it either?

    You assume there was a Q&A department to catch it in the first place. You also assume the shop uses tests.

    The very vact that this sort of thing gets through suggests to me they do have a QA dept - QA (in my exp) is useless - it tends to be a whole bunch of suits who can calculate meaningless statistics to satisfy Six Sigma, ITIL, CMMI or any other methodology/framework that helps convince themselves that they are somehow better off since spending the money and getting all their programmers trained in aforementioned framework - I especially love Six Sigma's 'Cost of Quality' vs 'Cost of Poor Quality' which boils down to a calculation of bugs vs potential bugs - Potential bugs would only include the ones they thought of, so they would (almost always) not exist - their results would be skewed because potential bugs could be as high or as low as you pleased (and how many undetected bugs may still exist that have not been discovered {and therefore aren't even potential bugs}?)

    QA not picking it up is no surprise to me... What does surprise me, though, is that presumably the code was reviewed and (hopefully) tested. How this ever made it to production remains a mystery....

  • Lustig (unregistered)

    I guess that company employs an innocent victim who has to explain to the overrated employee who wrote it why it doesn't work as expected. Of course they can run CPP to see what's wrong, but they do not have time to learn it, so they prefer to let someone smarter than them explain it forever. Well, instead of telling someone smarter than you that he or she is paid to explain you the basics of C++, RTFM!

  • (cs) in reply to Drande
    Drande:
    ObiWayneKenobi:
    Matt:
    I can understand someone making this mistake, but why didn't that person test what they wrote? Why didn't Q&A catch it either?

    You assume there was a Q&A department to catch it in the first place. You also assume the shop uses tests.

    The very vact that this sort of thing gets through suggests to me they do have a QA dept - QA (in my exp) is useless - it tends to be a whole bunch of suits who can calculate meaningless statistics to satisfy Six Sigma, ITIL, CMMI or any other methodology/framework that helps convince themselves that they are somehow better off since spending the money and getting all their programmers trained in aforementioned framework - I especially love Six Sigma's 'Cost of Quality' vs 'Cost of Poor Quality' which boils down to a calculation of bugs vs potential bugs - Potential bugs would only include the ones they thought of, so they would (almost always) not exist - their results would be skewed because potential bugs could be as high or as low as you pleased (and how many undetected bugs may still exist that have not been discovered {and therefore aren't even potential bugs}?)

    QA not picking it up is no surprise to me... What does surprise me, though, is that presumably the code was reviewed and (hopefully) tested. How this ever made it to production remains a mystery....

    Whilst not in any way disagreeing with your general observation (although I wish you'd mentioned Rational Robert), I have to point out that CMMI has little or nothing to do with statistics. It's a simple principle, really: your organisation is graded according to how closely you can approach the exact same result as the last time.

    Not only is this the most cretinous perversion of "process" I have ever encountered, but it has particular relevance to the code currently under discussion. I mean, CMMI only goes to level 5. This goes waaaay up there ... possibly even to level 11.

  • b@rt (unregistered)

    This is why students should be taught what goes on inside a compiler, even if invoking it is as trivial as pressing a single key. There are many, many bugs (or 'design issues') that could be prevented if the programmer only knew what's going on behind the scenes.

  • Bernie (unregistered) in reply to dpm
    dpm:
    Niels:
    Yeah, the #define in c/c++ are actually pre-compile time text processing. So this code will look something like this the moment it is actually offered fed into the compiler:

    void CReport::ToggleFilter() { }

    bool CReport::IsFilterEnabled() { return true; }

    <NITPICK>Actually it always returns false.</NITPICK>
    Actually, if
    #define ENABLE_FILTER
    is in globals.c and the rest is in CReport.cpp (and CReport.cpp doesn't include globals.c), then it returns true.

  • (cs)

    Bravo! That's one of the better WTF's I've seen on this site. It looks like clean code, it even makes sense to the casual reader, but it's oh so very wrong.

  • Will (unregistered) in reply to abx

    HAHAHAHAHAHA! do nothing.. hoooo... heheheh.

  • foxyshadis (unregistered) in reply to SuperousOxide
    SuperousOxide:
    beeblebrox:
    Why do you say that MFC is "legacy"? It is still being used a lot even in new programs.

    He's not saying MFC is legacy, he's saying these apps are legacy. They're no longer being actively developed, they just get called in for some spot fixes on them.

    Which coincidentally describes MFC itself, beeblebrox - it really is a legacy framework, even if it's still being used. People do new things with all kinds of legacy tools for all kinds of reasons.

  • (cs) in reply to m0ffx
    m0ffx:
    Q4T
    Matt:
    I can understand someone making this mistake, but why didn't that person test what they wrote? Why didn't Q&A catch it either?
    He tested it, found it didn't work, couldn't see what could possibly be wrong, gave up and moved on to somethigng else.
  • (cs) in reply to Rachelle
    Rachelle:
    cthulhu:
    I notice this article contains a Possibly this will continue with such wit as suggesting the function also return the string "Brilliant!".

    Its Brillant you n00b!

    There's something inherently ironic about calling cthulhu a n00b.

  • (cs) in reply to FIA
    FIA:
    [image] There, FTFY.

    The real WTF is that you printed 12 pages to get the one post you wanted?

  • Brwarner (unregistered)

    Wow... That's all I can say...Preprocessor toggle code...I mean it doesn't MAKE ANY SENSE!!!

  • The_Assimilator (unregistered) in reply to Brwarner

    Code like this makes me wish for android overlords to ruthlessly cull incompetents from society.

  • Guido (unregistered) in reply to abx
    abx:
    :O

    Sorry to break it to you, but

    The toggles... they do nothing

    Sorry for being dumb (or being a C(++) ignorant) but what is the problem with the code? Does undef or define not do what it says it does? From just looking at it first glance, it "seems" right...

    ashamed

  • Guido (unregistered) in reply to Guido
    Guido:
    abx:
    :O

    Sorry to break it to you, but

    The toggles... they do nothing

    Sorry for being dumb (or being a C(++) ignorant) but what is the problem with the code? Does undef or define not do what it says it does? From just looking at it first glance, it "seems" right...

    ashamed

    Now I'm even MORE ashamed...

    I admitted already I was a C-ignorant...

    The reason why I didn't read through the COMMENTS first to see what might be the WTF (which I NOW understand) is - they DID NOT SHOW...

    ashamed^2

  • gilty (unregistered) in reply to abx

    Compiler vs. Runtime understanding FAIL.

  • Kris (unregistered)

    Legacy software: the pre-'if' days!

  • (cs) in reply to foxyshadis
    foxyshadis:
    SuperousOxide:
    beeblebrox:
    Why do you say that MFC is "legacy"? It is still being used a lot even in new programs.

    He's not saying MFC is legacy, he's saying these apps are legacy. They're no longer being actively developed, they just get called in for some spot fixes on them.

    Which coincidentally describes MFC itself, beeblebrox - it really is a legacy framework, even if it's still being used. People do new things with all kinds of legacy tools for all kinds of reasons.

    No it isn't. Microsoft keeps adding to and improving MFC. Since Visual Studio 2008, it contains for example the Ribbon Control http://msdn.microsoft.com/en-us/library/bb984556.aspx Eat your heart out, you C-sharpies :-)

  • Nick (unregistered)

    It always annoys me to see code like this:

    function isSomethingTrue() { if (condition) { return true; } else { return false; } }

    when this would do:

    function isSomethingTrue() { return condition; }

    Not really the point of this WTF I know but I just had to get it off my chest

  • FIA (unregistered) in reply to bryan986
    bryan986:
    FIA:
    <snip image> There, FTFY.

    The real WTF is that you printed 12 pages to get the one post you wanted?

    LOL, no. Just the 1. 'Print preview' and the range selection bits of the print dialog are your friends. :)

    (Well, ok, I printed 3, but that's down to firefox refusing to print any of the comment text despite the preview looking just fine.)

  • (cs) in reply to abx
  • MichaelLJ (unregistered)

    I think someone toggled a concept in his brain:

    Preprocessor Logic vs. Runtime Logic

  • Locutus (unregistered) in reply to cthulhu
    cthulhu:
    Possibly this will continue with such wit as suggesting the function also return the string "Brilliant!".

    That would be "Brillant" ;)

  • (cs) in reply to ObiWayneKenobi
    ObiWayneKenobi:
    You assume there was a Q&A department to catch it in the first place.
    Q&A? What is that, a Question and Answer department?
  • kecho (unregistered)

    #define GOTO_COLLEGE

    #ifndefine GOTO_COLLEGE #define BUY_DEGREE #endif

    #if defined(GOTO_COLLEGE) graduate(); #else if defined(BUY_DEGREE) cheat_and_graduate(); #else bribe_and_graduate(); #endif

    he clearly didnt cheat or bribe anybody to graduate :)

  • diabol (unregistered)

    lol makro-madness

  • (cs) in reply to FIA
    FIA:
    [image] There, FTFY.
    TRWTF is that you printed out all twelve pages of comments to do that.
  • Wyrd (unregistered) in reply to dpm

    Yes, and this is important to me because it means that, while the code is definitely broken, the IsFilterEnabled() function would still return the correct value so that it is logically correct. Having something be broken and correct at the same time makes it more funny.

    Although at least one other poster has pointed out that this only works if the #define ENABLE_FILTER at the very top is in the same file (and precedes) ToggleFilter().

    -- Furry cows moo and decompress.

  • csharphacker (unregistered)

    University Education: $10 000 Desktop Computer: $3000 Visual C++ IDE: $500

    Not knowing the difference between a variable and a compiler directive: priceless

Leave a comment on “The Toggle that Wouldn't”

Log In or post as a guest

Replying to comment #:

« Return to Article