• Broonix (unregistered)

    its always a good idea to check everythign a few times before you process any data.... gag

    First Post!

  • Haora (unregistered)

    WTF?... I mean... I've heard of defensive programming, but this it just too much.....

  • Tack (unregistered)

    The only thing I can think of is that there were concurrency issues, and the original coder thought this would be a workable solution.  After all, if the value remains unchanged after three whole tests, surely any possible race conditions have been thwarted!

    Jason.

  • (cs) in reply to Tack

    Their boss probably told them to "measure twice, cut once", and they took it too much to heart.

  • (cs) in reply to Xargon

    paid by the line anyone?
    even if he wanted to do this, he could 've refactored it to a function tripleCheck()

  • (cs) in reply to Tack
    Anonymous:
    The only thing I can think of is that there were concurrency issues, and the original coder thought this would be a workable solution.  After all, if the value remains unchanged after three whole tests, surely any possible race conditions have been thwarted!


    I agree about concurrency.

    It depends on the compiler, but the extra checks might well be optimised away.

    Sincerely,

    Gene Wirchenko

  • Mark (unregistered)

    Is it me, or does this look like autogenerated code?

     

    Even still I can't understand anyone reading that and not thinking 'That's silly'.

     

    Question: Wouldn't whichever compiler/interpreter this was written for not optimise and remove the duplicate tests? That would mean it doesn't even work in the 'avoid race conditions' explanation.

  • Mark (unregistered) in reply to Gene Wirchenko

    Gene Wirchenko:
    Anonymous:
    The only thing I can think of is that there were concurrency issues, and the original coder thought this would be a workable solution.  After all, if the value remains unchanged after three whole tests, surely any possible race conditions have been thwarted!


    I agree about concurrency.

    It depends on the compiler, but the extra checks might well be optimised away.

    Sincerely,

    Gene Wirchenko

    Ah! Snap!

  • (cs) in reply to Gene Wirchenko
    Gene Wirchenko:
    ... but the extra checks might well be optimised away.


    For the sake of his users, I hope so...
  • Starfish (unregistered) in reply to t-bone
    t-bone:
    paid by the line anyone?
    even if he wanted to do this, he could 've refactored it to a function tripleCheck()


    Excellent idea!

    foreach(DataRow dr in organizations)
    {
    if (tripleCheck(dr[FieldMap.Organization.is_active]))
    {
    if (tripleCheck(dr[FieldMap.Organization.is_active]))
    {
    if (tripleCheck(dr[FieldMap.Organization.is_active]))
    {
    addOrgRow(dr);
    }
    }
    }
    }
    With thrice as much error checking, it must be foolproof.
  • jeffk (unregistered) in reply to Mark

    I agree Mark. That's definately a bug in an autogen tool. That's why we always proof read autogened code, right?

  • (cs) in reply to Haora

    Looks to me like this could be protection from race conditions by someone who doesn't really grep threaded programming.

    ...but if you're going to do that like this, you need to put time delays between each check. Otherwise there's not much point in the repeated checks.

    Just stick some { short i; while(i >= 0) i++; } blocks in there between the 1st & 2nd, and 2nd & 3rd, of each of the triple checks. WTF solved.

    (let's see who doesn't realise I'm kidding...)

  • SpecialEd (unregistered)

    This is why you shouldn't use copy-and-paste-paste-paste programming.

  • (cs) in reply to JBL

    Obviously this was for a goverment job.  He is just checking all three forms of the required triplicate format.
    pi

  • Loren (unregistered)

    Agreed--this was written by someone who doesn't know how to handle shared access to files.  Locking mechanisms exist for a reason!

    On the other hand, I've writtten code not too different when dealing with stupid hardware.  No sharing issues, just slow to respond.  By checking multiple times you could ensure that it wasn't just about ready to tell you no.  A delay would have accomplished the same thing but getting a delay loop that behaved on CPU's of widely different speeds would be a problem but by hitting the offending hardware you got a much more predictable delay.

  • (cs) in reply to Disgruntled DBA

    seems to me the guy heard something about race conditions... it's common to see duplicated code to try and prevent race conditions (which doesn't really help, but kinda makes you think it does)

  • boogieman (unregistered)

    Maybe the original programmer had obsessive compulsive disorder.
    You know, like those guys who always have to turn the light on 3 times when they enter a room?

  • Mark (unregistered) in reply to boogieman

    It brings a whole new meaning to 'ternary operator' doesn't it? [:P]

  • (cs) in reply to SpecialEd

    Anonymous:
    This is why you shouldn't use copy-and-paste-paste-paste programming.

     

    Rofl. Thanks, you made my day ;)

  • Randolpho (unregistered) in reply to Mark
    Anonymous:

    Is it me, or does this look like autogenerated code?

    My first throught as well. Buggy auto code generation.

    In fact, it reminds me of the way Frontpage used to generate HTML.....

  • (cs)

    But did anyone check the "is_active" method

    function is_active bool ( return randomize(true|false); )

    If you're lucky enough to get three trues in a row, the subroutine will run add{object}Row().

    Maybe it's a random way to populate a set of tables.

  • Mike5 (unregistered)

    Tsk, tsk, tsk.Fresh out college and already so drunk on the job, he's seeing in triplets...

  • (cs) in reply to BlackTigerX

    BlackTigerX:
    seems to me the guy heard something about race conditions... it's common to see duplicated code to try and prevent race conditions (which doesn't really help, but kinda makes you think it does)

     

    All this talk about race conditions on the day after MLK day... coincidence?

  • an apprentice (unregistered) in reply to Irrelevant

    Now, the first post about preventing concurrency problems made me chuckle, but after having read that 'explanation' for the fourth time I feel that some people don't just mean sarcasm.

    How exactly does this WTF prevent race conditions? If the variable can be messed with in between being checked and being altered, then it can as well be messed with between being checked for the third time and being altered.

    Please enlighten me if there is actually any point in this WTF that I miss.

  • Villa (unregistered) in reply to MrVJTod

    The creator of that code was probably dealing with the highly skilled assassin Mustafa. Austin Powers, anyone?

  • (cs) in reply to Randolpho

    My vote is for bad autogen code as well. Even the best code generation systems end up with some WTFs like these from time to time.

  • (cs)
    Alex Papadimoulis:

    foreach(DataRow dr in organizations)
    { 
      if ((bool)dr[FieldMap.Organization.is_active]) 
      { 
        if ((bool)dr[FieldMap.Organization.is_active]) 
        {
          if ((bool)dr[FieldMap.Organization.is_active]) 
          {
            addOrgRow(dr);
          }
        }
      }
    }


    It's obvious to me what's going on here.  The programmer did it like this for a year or two.  Then all of a sudden he switched to only doing one check.  Why, you ask?  So that he could say that he tripled his productivity when annual performance reviews came up!  Brillant!
  • (cs)
    Alex Papadimoulis:
       if ((bool)dr[FieldMap.Organization.is_active]) 
    {
    addOrgRow(dr);
    }


    I'm curious to know what the hell he is doing casting
    FieldMap.Organization.is_active

    to a bool. Isn't is_active already a bool?
    If it isn't, what does the cast accomplish anyway? Does he think that (bool)x is false when x is 0 and
    true when x is 1?

    WTF?!
    I tried really hard to come up with an explanation as to why he checked everything three times but I just can't figure it out.
    I don't buy the BS (I'm also not sure if people are kidding or not) about concurrency.
    Seriously, do you think that someone who writes code like this has heard about concurrency issues?
    It's amazing the guy has a job at all!
  • hahanoob (unregistered) in reply to Gene Wirchenko
    Gene Wirchenko:
    Anonymous:
    The only thing I can think of is that there were concurrency issues, and the original coder thought this would be a workable solution.  After all, if the value remains unchanged after three whole tests, surely any possible race conditions have been thwarted!


    I agree about concurrency.

    It depends on the compiler, but the extra checks might well be optimised away.

    Sincerely,

    Gene Wirchenko



    What the hell is wrong with you? Stop signing your posts.

    Sincerely,

    Humbly,

    With Love,

    Regards,

    hahanoob
  • uncool (unregistered)

    he needs a fourth check

    we all know bad things happen in 3's

  • Bill307 (unregistered) in reply to an apprentice
    Anonymous:

    Now, the first post about preventing concurrency problems made me chuckle, but after having read that 'explanation' for the fourth time I feel that some people don't just mean sarcasm.

    How exactly does this WTF prevent race conditions? If the variable can be messed with in between being checked and being altered, then it can as well be messed with between being checked for the third time and being altered.

    Please enlighten me if there is actually any point in this WTF that I miss.

    It doesn't prevent race conditions.  What people are saying is that to certain, less-skilled programmers, it may appear to prevent race conditions, but in reality it does not.

    Coincidentally, I notice that the Size and Font dropdown lists above the reply area default to "3" and "Times" ;).

  • The arch-achitect (unregistered)

    Where's the specs ?

  • YouDidn'tSeeAnything (unregistered)

    So WTF did he replace it with?  It'd be much better to scoff at the existing code if it was successfully replaced with something better.  Otherwise, the WTF'er is still clueless as to WTF the problem is that caused the original coder to do three checks in the first place.

  • (cs)

    Obviously from the Lewis Carroll school of programming:

    "I told you once,
    I've told you twice,
    I've said it to you three times,
    and what I tell you three times is true."
    Lewis Carrol, the Hunting of the Snark.

  • (cs) in reply to hahanoob

    Anonymous:


    What the hell is wrong with you? Stop signing your posts.

    Sincerely,

    Humbly,

    With Love,

    Regards,

    hahanoob

    I'd say I agree with you, but you know what they say about arguing on the internet....

  • Alex (unregistered) in reply to zip

    Probably some really ghetto and stupid way to be thread safe.

  • (cs) in reply to ferrengi
    ferrengi:
    Alex Papadimoulis:
       if ((bool)dr[FieldMap.Organization.is_active]) 
    {
    addOrgRow(dr);
    }


    I'm curious to know what the hell he is doing casting
    FieldMap.Organization.is_active

    to a bool. Isn't is_active already a bool?
    If it isn't, what does the cast accomplish anyway? Does he think that (bool)x is false when x is 0 and
    true when x is 1?

    WTF?!
    I tried really hard to come up with an explanation as to why he checked everything three times but I just can't figure it out.
    I don't buy the BS (I'm also not sure if people are kidding or not) about concurrency.
    Seriously, do you think that someone who writes code like this has heard about concurrency issues?
    It's amazing the guy has a job at all!


    He's actually casting dr[], which is another wtf, using a boolean for accessing an array.
  • (cs)

    bad! Bad code generating bot!!!!

    Looks like the result of a flakey uml to code generator or something equally demented. The matrix and asimov talk about when computers and programs get so complicated only other computers can write them. Bah, I say. If it's screwed up in the first generation, progressive generations will look more and more like the cast from deliverance.

    Computers hillbillies would regret ever having us write the first generation but be too stupid to fix the problem.

    Garbage in, garbage out as they say.

    That said, if you're gotta let something generate code for you, at least check that it produces something reasonable. I've been told about java code where each level of inheritance only added a single property/method. Stupidity of that degree is too time consuming to be anything BUT the result of a computer output from modeling software.

  • toxik (unregistered)

    You can count my vote to the autogen-theory.
    I mean seriously, no one is as stupid as to write that by hand or even copy&paste, as for accesing an array with a boolean; think again, who said is_active is a boolean?

  • toxik (unregistered)

    Oh and let me laugh at the CAPTCHA.

    I had the word to type in AutoComplete, and I've posted 10 posts maximum.

  • (cs)

    pfft.  The real wtf is the complete lack of error handling:

    <font size="3">bool unthinkableMayhem = invert_bool(true);
    </font>

    <font size="3">foreach(DataRow dr in organizations){ 
    if ((bool)dr[FieldMap.Organization.is_active]){
    if ((bool)dr[FieldMap.Organization.is_active]){
    if ((bool)dr[FieldMap.Organization.is_active]){
    addOrgRow(dr);
    }else{
    unthinkableMayhem = invert_bool(false);
     }
    }else{
    unthinkableMayhem = invert_bool(0);
    }
    }else{
    unthinkableMayhem = invert_bool(-0);
    }
    }
    if (invert_bool(unthinkableMayhem)){
    return "Brillant";
    }else{
    return "FileNotFound";
    }
    return;</font>



  • Eric (unregistered) in reply to toxik

    I'm suprised no one said much about the []'s.  He could have overloaded them to mean anything.  Also, you don't know what dr is, only that it has DataRows.  Maybe accessing it one time will cause something to happen, the second is a retry, and if it doesn't work by the third then just forget it.

    Even if this is the case, still a real stupid way to go about it.

  • Ryan (unregistered)

    No way. You're all giving him way too much credit. This guy thinks thread safe means "without needles".

    More likely the boss announced that productivity will be measured in lines of code and nothing more.

  • killer (unregistered)

    I think there is another explanation that hasnt been tossed out there ...

    is it possible this code was corrupted by some revision control system, someone applying some patches that happened to get applied 3 times?

  • another guy (unregistered)

    I've seen production code like this at my previous project. For some reason one developer thought that executing certain SLQ update statement (in Access VBA [:^)] ) couple of times, as opposed to just once,  really really updates. So some not all updates statement were wrapped in for loop. I am not sure what criteria this developer used to determine which updates qualified for that. Frankly, I was afraid to ask [:)]

  • Runtime Error (unregistered) in reply to Xepol
    Xepol:
    bad! Bad code generating bot!!!! Looks like the result of a flakey uml to code generator or something equally demented. The matrix and asimov talk about when computers and programs get so complicated only other computers can write them. Bah, I say. If it's screwed up in the first generation, progressive generations will look more and more like the cast from deliverance. Computers hillbillies would regret ever having us write the first generation but be too stupid to fix the problem. Garbage in, garbage out as they say. That said, if you're gotta let something generate code for you, at least check that it produces something reasonable. I've been told about java code where each level of inheritance only added a single property/method. Stupidity of that degree is too time consuming to be anything BUT the result of a computer output from modeling software.



    I see a future where seintient AI programs clutered with WTF's search the internet for rogue programmers that will put them out of their misery.

  • (cs) in reply to Mark
    Anonymous:
    Is it me, or does this look like autogenerated code?

    That was my first thought too. But for sure a bad generator.
  • Viewer (unregistered) in reply to Irrelevant
    Irrelevant:
    Looks to me like this could be protection from race conditions by someone who doesn't really grep threaded programming.

    Looks to me like this could be written by someone who doesn't really grok the difference between "grep" and "grok".
  • (cs) in reply to Tack
    Anonymous:
    The only thing I can think of is that there were concurrency issues, and the original coder thought this would be a workable solution.  After all, if the value remains unchanged after three whole tests, surely any possible race conditions have been thwarted!

    My guess is that the code at one time checked three different things. Perhaps the same field in the different instances or something. And a design change got made and someone went through and mechanically changed all references to one instance another and ended up with three checks that were excactly the same. But the person was just doing mindless replacement and didn't notice and so didn't bother to collect them together.

  • (cs)

    What if the == operator was overloaded to have side effects?

Leave a comment on “Ask Thrice and Ye Shall Receive ”

Log In or post as a guest

Replying to comment #:

« Return to Article