• (cs) in reply to Submitter

    Submitter:

    ...
    4) The explanation i received was that the value in the DB might have changed after the first if statement so they checked it again... twice.  Why only twice? I don't remember if i asked that or not.  The funny part about this is the values are already pulled from the db and are in the DataRow, and will not change unless the DataRow is repopulated, so there is absolutely no reason to check it three times.
    ...

    Okay, but what if they're using Banker's Boolean for the variable?

    Nonetheless, this practice is not recommended, as it can make your processor neurotic. "Is the Organization active? Then go do --wait, is it still active? Right, so do --is it active now?" Run that a couple hundred times, and your computer will start developing ticks, like opening and closing the CD drive spontaneously.

    --Rank
    --Rank
    --Rank


     

  • Submitter (unregistered) in reply to Submitter
    Anonymous:
    To debunk all the theories.....

    1) It's C#.  dr[FieldMap.Tag.is_active] returns what value was in the is_active column of the Tag table as an object, hence the cast to a bool.

    2) It wasn't generated by a code-generator, it was hand written.

    3) I wasn't drunk when i was first reading the code.  I wanted to get drunk immediately afterward, but lunch wasn't for another hour.

    4) The explanation i received was that the value in the DB might have changed after the first if statement so they checked it again... twice.  Why only twice? I don't remember if i asked that or not.  The funny part about this is the values are already pulled from the db and are in the DataRow, and will not change unless the DataRow is repopulated, so there is absolutely no reason to check it three times.

    I believe it was due to copy/paste, but will probably never know.

    Of course, this code is from the same guy who chose to implement the entire project without using form designer because he believed the .res files that the form designer generated created too much overhead.  This was the main reason for the file being 28,600 lines (yes, exactly 28,600.)


    5) He was not paid by the line.
  • sneaky (unregistered) in reply to Submitter

    <FONT face="Times New Roman" color=#000000 size=3>I disassembled the object class to see if this might shed some light on the topic. Imagine my surprise when i found this...</FONT>

    public class object
    {
       ...

       private static int wtf = 0;

       public static explicit operator bool(object o)
       {
          if (wtf++ < 2)
             return false;
          wtf = 0;
          return true;
       }

       ...
    }

     

  • Arjan (unregistered) in reply to Viewer
    Anonymous:
    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".


    Is that a global regular oxprossion krinter?
    :D
  • (cs) in reply to Viewer

    Viewer:
    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".

    Real coders use grep to grok.

  • (cs) in reply to Viewer
    Anonymous:
    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".

    Touché.

    In my defence, I'd put that down as one of the side-effects I've been suffering from being stuck on windows machines (because I don't actually own them).
  • (cs) in reply to boogieman
    Anonymous:
    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?


    That was my first thought when I saw this.  According to the submitter, the author of the code was not a stupid person, and the code was not auto-generated, so this seems likely...

    I once worked with an ocd-sufferer on a asp site.  Seeing a comment starting with only one single quote (the comment character in vb) drove him absolutely crazy, so he went through the code on a regular basis to replace every ' in a comment with '' (two single quotes).

    I know a few people with ocd so I had some sympathy for the guy.  Still, I always changed the comments back on any file I was working with just to fuck with him.

  • (cs)

    I remember having a hardware which had (due to an incompatibility of operating system and processor step) the habit of occasionally miscalculating results of multiplications. As the error could appear only every 10ms, we simply multiplied everything thrice and took the result which appeared two times...
    I still love this solution even though with more experience now, I'm sure, that another solution could have been found :-)

  • (cs) in reply to jmo
    Anonymous:

    Darax The Good:
    What if the == operator was overloaded to have side effects?

    it doesnt use the == operator though!

    the if implies a comparison...

    I suppose it might be a > 0 comparison-but for a bool it likely is an == true.

  • (cs) in reply to Bingo
    Anonymous:
    Anonymous:
    Anonymous:
    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".


    Good for you, Michael Smith


    Was that Michael Valentine Smith?


    Valentine Michael Smith, actually. Reading that as we speak.
  • weioajfijaw (unregistered) in reply to b1xml2
    b1xml2:
    Therein lies the difference between an ignoramus like your good self and those that take some time to look under the hood. It is faster to use a DataView then to do the loop.
    Please tell me, oh master of ADO.NET, exactly what's happening then.  Please tell me how ADO.NET can somehow optimize a search ON A BIT FIELD.  A BIT FIELD.  You know, those fields with LOW, LOW SELECTIVITY (there's only two possible values on a non-nullable field).  Please tell me how any type of search is somehow going to be faster, dumbass.
     
     
  • GPG (unregistered) in reply to Gene Wirchenko

    I am not sure about this code being able to handle concurrency or race conditions.  Consider - it must pass the same test three times in order to execute the desired instruction - BUT if any of the tests fail, it does not wait and retry.  The next data item is processed, thus missing the target item.

    Ugly for certain.

  • (cs) in reply to Randolpho
    Anonymous:
    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.....

     

    you ever play with the netscape html editor ..

    ahh, <big><big><big>Lovely</big></big></big>

  • (cs) in reply to Runtime Error
    Anonymous:

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


    So our future is to essentially become AI surgeon-therapists?
  • tim (unregistered)

    you fucking what?

  • (cs) in reply to weioajfijaw

    Firstly, go get a book on the .NET Framework. Read it and understand it.
    Secondly, realise that this is more likely than not version 1.1 and not version 2.0
    Thirdly, the performance hit is there because of
    1. Boxing and unboxing
    2. Casting

    #1 happens with the loop
    #2 happens with the bool casting
    In particular, boxing and unboxing is a rather expensive operation. Hence generics in .NET 2.0.

    So to answer such an ignoramus as you, there is a material difference both in pattern designing as well as performance when you use a DataView to get 50 records from 50,000 as opposed to looping the entire 50,000 records and applying the cast.

    You obviously have either weak knowledge of the .NET fundamentals or you are just trolling.

    Anonymous:
    b1xml2:
    Therein lies the difference between an ignoramus like your good self and those that take some time to look under the hood. It is faster to use a DataView then to do the loop.
    Please tell me, oh master of ADO.NET, exactly what's happening then.  Please tell me how ADO.NET can somehow optimize a search ON A BIT FIELD.  A BIT FIELD.  You know, those fields with LOW, LOW SELECTIVITY (there's only two possible values on a non-nullable field).  Please tell me how any type of search is somehow going to be faster, dumbass.

     
     
  • (cs) in reply to weioajfijaw

    And obviously, you have not even bothered to look under the hood.

    Besides, the issues mentioned, there is quickly algorithms under the hood to do the row filtering. Go get yourself Lutz's .NET Reflector and look at the code.

    It amazes me that you have not talked about any piece of .NET code to support your arguments, merely specious generalisations which would lead one to belief you are not even a .NET programmer

    Anonymous:
    b1xml2:
    Therein lies the difference between an ignoramus like your good self and those that take some time to look under the hood. It is faster to use a DataView then to do the loop.
    Please tell me, oh master of ADO.NET, exactly what's happening then.  Please tell me how ADO.NET can somehow optimize a search ON A BIT FIELD.  A BIT FIELD.  You know, those fields with LOW, LOW SELECTIVITY (there's only two possible values on a non-nullable field).  Please tell me how any type of search is somehow going to be faster, dumbass.

     
     
  • (cs) in reply to Anonymous
    Anonymous:

    You are not reading it wrong, you are just interpreting it wrong.  The C# language, while it can create enumerations of different underlying types (of integral values, specifically), does not implicitly cast enumeration values to that underlying type when assigning or passing as parameters.  From the MSDN documentation on C# enumerations:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcrefTheEnumerationTypes.asp

    Note the section under 'Remarks' which reads: "The underlying type specifies how much storage is allocated for each enumerator. However, an explicit cast is needed to convert from enum type to an integral type."



    Many thanks for pointing this out :-)  I'll try to remember that next time I play with C#.
  • Dwonis (unregistered) in reply to akrotkov
    akrotkov:

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


    Who says it's a boolean?  I bet it's an enum.
  • Dwonis (unregistered) in reply to Guest
    Anonymous:
    t-bone:
    paid by the line anyone?
    even if he wanted to do this, he could 've refactored it to a function tripleCheck()


    Ah, but if he was going to refactor the chek out to a function then surely you want to be certain...

    function makeAbsolutelySure(bool test) {
        return makeAbsolutelySure(test)
    }

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



    The trick is to get the linker to put makeAbsolutelySure (or is it makeAbsolutelyCertain?) right before the stack space.  With any luck, when the stack overflows, it will overwrite the right instructions, causing makeAbsolutelySure to return 0...
  • Anonymaly (unregistered) in reply to eimaj2nz
    eimaj2nz:
    Anonymous:

    You are not reading it wrong, you are just interpreting it wrong.  The C# language, while it can create enumerations of different underlying types (of integral values, specifically), does not implicitly cast enumeration values to that underlying type when assigning or passing as parameters.  From the MSDN documentation on C# enumerations:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcrefTheEnumerationTypes.asp

    Note the section under 'Remarks' which reads: "The underlying type specifies how much storage is allocated for each enumerator. However, an explicit cast is needed to convert from enum type to an integral type."



    Many thanks for pointing this out :-)  I'll try to remember that next time I play with C#.

    Now now.... not everyone gets the chance to play and get paid good money at the same time.  The devil is usually in the details and it certainly doesn't hurt to know them. <GRIN>

  • D (unregistered) in reply to Eric

    Yeah, side effects ...

    Like in C:

      if (i++ = 3){
         if (i++ = 3) {
             if (i++ = 3) {
                 printf ("Ha Ha!\n");
              }
          }
      }

  • The Master Idiot (unregistered) in reply to b1xml2

    Too much code snipped from the original to show what is going on "under the hood". You are right tho, there is NO need for a loop here to get at 50 required records when there could be 50,000. This is where data access logic should of been written to look for the is_active field IN the query. Obviously, it wasn't. For a better assessment of the situation, you would need to really look at the business rules, perhaps they would yield a more robust picture of expected returned data based on criteria other than the is_active field... Perhaps instead of 50,000 records it was only 10. Looking for a couple records with is_active is a moot point. On the other hand, in my experience is_current and is_active have always been a small selection of records, sometimes small enough to impart <.5% filter selectivity, thereby even making an index on something with such a poor cardinality (field-wise) perhaps a good choice. In this situation, you would be a fool not to include this in your data access logic.

    Still no need for a DataView in the part of the app we are currently discussing. Then again, there was no need to use DataSets either except that it was easy to implement. I still stand by DataReaders, as this is what both DataViews/DataSets utilize under the hood, yet without all the overhead, that is, UNLESS you need most of it, in which case, it is no longer overhead.

    Then again, I've been living in a world of delphi win32 as of late, so maybe my C# is gettin rusty. :D


  • Annoyed (unregistered) in reply to Gene Wirchenko
    Gene Wirchenko:
    Anonymous:
    Anonymous:
    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.

    No it would not, or at least I hope not. The compiler looks at code and checks it for logical/structural errors( if command without then) and internal reserved words/commands (do = do +1).
    <o:p>

    </o:p>

    <o:p>The language need not have reserved words.  FORTRAN is an example of this.  I believe PL/I also is.
    </o:p>

    <o:p>A compiler can do far more than that limited checking.  How much it can get away with depends on the language.
    </o:p>

    Sincerely,

    Gene Wirchenko



    Please stop it! You clearly don't realise how utterly dorky you read. It's bordering on physical pain.

    Sincerely,

    Love and kisses,

    Mr James T Peterson III MBA TCS LMN LPL
    ---------------
    Sincerely,

    Love and kisses,


    Mr James T Peterson III MBA TCS LMN LPL

  • (cs) in reply to The Master Idiot

    Typical when nothing insightful to add, you try to muddle the issue and obfuscate.

    1. With what was presented in the code, it is by far preferable to use DataViews as opposed to the DataTable and the looping.
    2. With boxing and unboxing occuring during the loop (with the use of the Enumerator), there is a penalty to pay when looping through 50,000 DataRows in the DataRowCollection to get at 50.

    Therefore, ipso facto, DataViews are the right way to go here. Of course, if we want to talk of design patterns, and software architecture, we can do completely without anything from the System.Data namespace and use our own Domain objects. However, the code is NOT there for this, so such statements can only come when you have got nothing insightful or good to add.

    Because, the records are being passed as DataRows, we can but assume there is a need to hold the data in-memory. DataViews are the preferred way to handle the manner in which the rows required are discovered. Your assertion against DataViews is specious and lack clear understanding. As you say, stick to Delphi if your C# is rusty, and it looks like your C# is indeed rusty,

    Anonymous:

    Too much code snipped from the original to show what is going on "under the hood". You are right tho, there is NO need for a loop here to get at 50 required records when there could be 50,000. This is where data access logic should of been written to look for the is_active field IN the query. Obviously, it wasn't. For a better assessment of the situation, you would need to really look at the business rules, perhaps they would yield a more robust picture of expected returned data based on criteria other than the is_active field... Perhaps instead of 50,000 records it was only 10. Looking for a couple records with is_active is a moot point. On the other hand, in my experience is_current and is_active have always been a small selection of records, sometimes small enough to impart <.5% filter selectivity, thereby even making an index on something with such a poor cardinality (field-wise) perhaps a good choice. In this situation, you would be a fool not to include this in your data access logic.

    Still no need for a DataView in the part of the app we are currently discussing. Then again, there was no need to use DataSets either except that it was easy to implement. I still stand by DataReaders, as this is what both DataViews/DataSets utilize under the hood, yet without all the overhead, that is, UNLESS you need most of it, in which case, it is no longer overhead.

    Then again, I've been living in a world of delphi win32 as of late, so maybe my C# is gettin rusty. :D


  • Al (unregistered)

    Wasn't this already done in The Search for the Holy Grail?

    "Three shall be the number...

  • (cs) in reply to DevNull
    DevNull:
    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.


    Ouch!  Misquoting it is one thing, and removing any hint of a rhyme scheme is another, but did you have to totally destroy the meter, too?  I think you owe Charles an apology.

    “Just the place for a Snark!” the Bellman cried,
            As he landed his crew with care;
    Supporting each man on the top of the tide
            By a finger entwined in his hair.

    “Just the place for a Snark! I have said it twice:
            That alone should encourage the crew.
    Just the place for a Snark! I have said it thrice:
            What I tell you three times is true.”

  • (cs) in reply to Annoyed
    Anonymous:
    Gene Wirchenko:
    Sincerely,

    Gene Wirchenko



    Please stop it! You clearly don't realise how utterly dorky you read. It's bordering on physical pain.

    Sincerely,

    Love and kisses,

    Mr James T Peterson III MBA TCS LMN LPL (Mrs)



    I find it quaint and utterly charming.  Keep it up, Gene.  Nolite te bastardes carborundorum.

    Cheers,
    Dave Conrad

  • (cs) in reply to Al
    Anonymous:
    Wasn't this already done in The Search for the Holy Grail?

    "Three shall be the number...

    That's actually an excerpt the Book of Armaments (chapter 2 verses 9-21)

    And the LORD spake, saying, first shalt thou take out the Holy Pin. Then, shalt thou count to three. No more, no less. Three shall be the number thou shalt count, and the number of the counting shall be three. Four shalt thou not count, nor either count thou two, excepting that thou then proceed to three. Five is right out. Once the number three, being the third number, be reached, is counted, then lobbest thou thy Holy Hand Grenade of Antioch towards thy foe, who, being naughty in My sight, shall snuff it.
  • (cs)

    I'm almost curtain that the actual bug is in the code generator that generated this code.

    Apparently there are always 3 checks that should be done. Either the generator can't handle just one check configured in some configuration file or even worse, it iterates through the config items, but the index is not set to get the right value.

    Lesson is: take building a code generator as serious as the application it's generating code for.

  • dooood (unregistered) in reply to b1xml2

    heh, never would want to believe such a cock-smoking jizz-gobbler could be right, but sometimes they are.

    Man, ur co-workers must love ya! :D

    PS: Somebody get this poor soul some friends!



  • Moobar (unregistered) 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!

    Jason.


    This doesn't make sense.  He's attempting to read the value, not write it.  You don't get a race condition from read accesses.
  • David Schwartz (unregistered) in reply to Moobar

    "This doesn't make sense. He's attempting to read the value, not write it. You don't get a race condition from read accesses."

    You might if someone else writes to it while you're trying to read from it. Google "word tearing" for just one example of how this can happen.

    An absurd, but likely to work most of the time, method for avoiding word tearing might be to read three times and reject the outlier. That would still be a WTF though.

  • Old Mean Man With No Legs (unregistered)

    I am going to post this three times just to make sure it gets posted.

  • Old Mean Man With No Legs (unregistered)

    I am going to post this three times just to make sure it gets posted.

  • Old Mean Man With No Legs (unregistered)

    I am going to post this three times just to make sure it gets posted.

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

Log In or post as a guest

Replying to comment #:

« Return to Article