Reversing List



  • A friend of mine, let's say Joe, was using our company's internal TFlexList structure expecting a list structure where Append() added items to the end, Insert() added items to the start, GetFirst() got the first element, and GetLast() would get the last element. So if you did this  (pseudocode):

    Append("a");
    Append("s");
    Append("d");
    Append("f");

    Then pulled the items out using:

    GetFirst();
    GetNext();
    GetNext();
    GetNext();


    You'd expect "a", "s", "d", "f", right? Well, he was getting "f", "d", "s", "a" for some reason. Looking over his code a number of times, everything seemed to be in order, so he looked at the documentation and this is what he found:


    //////////////////////////////////////////////////////////////////////////
    //                              CLASS
    //
    // Class: TFlexList
    //
    // Super Class:
    //
    // Documentation:
    // This class manages the linked list of TFlexItems.
    //
    // The list is created in the following manner:
    //
    // +-- AppendContext()             InsertContext() ----+
    // |                                                   |
    // +-- Append()    TrueInsert()---+       Insert() ----+
    // |                              |                    |
    // v +----------------------------v------------------+ v
    //   |  TAIL  |                             |  HEAD  |
    //   +-----------------------------------------------+
    //       ^                                      ^
    //    GetFirst()                           GetLast()
    //
    //           GetNext()  moves toward HEAD   ----->
    //           GetPrev()  moves toward TAIL   <-----
    //
    //           TrueInsert will insert towards the head end of current.
    //           
    //           AppendContext() and InsertContext() will place a
    //           context from a list into the list at the appropriate
    //           position. If the context was in a list, then it will
    //           be unlinked.
    //

     

    At least it was documented.



  • Perhaps the developer had his head up his tail when he wrote this...



  • I guess you can say this developer didn't know his heads from his tails!

    That was supposed to be a joke.




  • // +-- AppendContext() InsertContext() ----+
    // | |
    // +-- Append() TrueInsert()---+ Insert() ----+
    // | | |
    // v +----------------------------v------------------+ v
    // | TAIL | | HEAD |
    // +-----------------------------------------------+
    // ^ ^
    // GetFirst() GetLast()



  • The weirdest thing is that he managed to write the whole thing without reallizing how fucked up this is.  All he would really have to do is switch the names of append and insert.



  • @DescentJS said:

    The weirdest thing is that he managed to write the whole thing without reallizing how fucked up this is.  All he would really have to do is switch the names of append and insert.

    The weirdest thing is I've seen a couple of these.  Neither were quite so well documented, but it was clear that the second dev along noticed the WTF - but, by that point, enough code was implemented that used the stuff, he1 felt safer just documenting it.



  •  Maybe he just flipped a coin to determine which end of the list would be called "heads" and which would be "tails."



  • @barfoo said:

     Maybe he just flipped a coin to determine which end of the list would be called "heads" and which would be "tails."

    Maybe he flipped two coins, one for each end.  Just be thankful the list didn't end up with two HEADs.  



  • Could this be something to do with languages that are read from right to left?



  •  The only real question now is... what is the function to get head?



  • @paulgeering said:

    Could this be something to do with languages that are read from right to left?

    I can't speak for this particular case, but the two similar cases I've encountered only involved a single person who was not a protestant Caucasian American, and that individual was not one of the original authors.  On the other hand, one of the original authors was described as being, "not really fluent in any language."  (I did not meet either original author.  I met one of the second authors, and the fifth author of the other project.  I ended up being the sixth and last author for that project - well, at least, that fork of it.  Thanks to the wonders of acquisitions and divestitures, there may still be other forks.)



  • @DOA said:

     The only real question now is... what is the function to get head?

     


    void getHead() {

       if( girl.isDrunk() )

            dragGirlToBathroom();

       else

            buyGirlDrink();

    }



  • @amischiefr said:

    @DOA said:

     The only real question now is... what is the function to get head?

     


    void getHead() {

       if( girl.isDrunk() )

            dragGirlToBathroom();

       else

            buyGirlDrink();

    }

    You forgot to place a recursive call to getHead() after buyGirlDrink();



  • @toth said:

    void getHead() {

       if( girl.isDrunk() )

            dragGirlToBathroom();

       else

            buyGirlDrink();

    }

    Recursive is bad mmkay

    void getHead() {
    
       while( !girl.isDrunk() )
            buyGirlDrink(); 
    
       dragGirlToBathroom();
    } 
    


  •  @amischiefr said:

    @DOA said:

     The only real question now is... what is the function to get head?

     


    void getHead() {

       if( girl.isDrunk() )

            dragGirlToBathroom();

       else

            buyGirlDrink();

    }

     

    At least THIS still works when Head and Tail are reversed:

     

    void getTail() {

       if( girl.isDrunk() )

            dragGirlToBathroom();

       else

            buyGirlDrink();

    }

     


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.