Humorous Code

This is a discussion on Humorous Code within the General Discussions forums, part of the Community Boards category; We've all had some instances in our lives where we have either written or seen humorous code. Take this chance ...

  1. #1
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738

    Humorous Code

    We've all had some instances in our lives where we have either written or seen humorous code. Take this chance to talk about some instances (and even post code examples) of when you have written some real humorous code.

    Some humorous code is obviously fake and contrived, such as the the many examples of the Windows source code we see around the internet. Don't post stuff like that.

    My Example

    Today I was writing some code to work with the ScrollViewer control in WPF and C#. I've subscribed to an event, so that every time the event fires some code should be executed, but I only want the code to be executed when the "ExtentWidthChange" variable is non-zero.

    Since I don't trust comparing double values to 0.0 directly (I won't get into why), I convert it to an int to get a solid 0 value. Here's the code:

    Code:
    void MyScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
    {
    	int changeWeCanBelieveIn = Convert.ToInt32(e.ExtentWidthChange);
    	if (changeWeCanBelieveIn != 0)
    	{
    		MyScrollViewer.ScrollToHorizontalOffset(_percentScrollHorizontal * MyScrollViewer.ScrollableWidth);
    		MyScrollViewer.ScrollToVerticalOffset(_percentScrollVertical * MyScrollViewer.ScrollableHeight);
    	}
    }
    (If you don't see the humor in that code, shame on you)

    Post your own examples of humorous code...
    Last edited by DavidP; 08-26-2010 at 11:11 AM.
    My Website

    "Circular logic is good because it is."

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,665
    Sorry, I just don't get it. I know what your doing, but I don't see what's so funny. I also don't check floats for 0.0 - but I still don't get the 'humor'.

    Is it because, in theory, the event wouldn't even be called in the first place if the event was zero?
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    I wrote something like this recently:

    Code:
    void MyClass::Func()
    {
       if (m_flag)
       {
            Foo();
       }
    }
    
    void MyClass::Foo()
    {
        if (m_flag)
        {
             ...
        }
    }
    Guess I was just trying to make sure that m_flag was indeed set before executing Foo(). Thankfully a co-worker caught it in a review and I changed it. He asked if my second check was just a sanity check or what. I told him it was more like an insane check.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    A looooong time ago before I understood (C++) references very well, I was writing a test harness which tests an API library my company was making, and I did something like this:
    Code:
    SomeClass* pFoo = NULL;
    APIClass.FuncThatTakesRef( *pFoo );
    To try to break the API by passing a null reference. Suffice it to say, the API developers made fun of that code for years afterwards.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by Bubba View Post
    I wrote something like this recently:

    Code:
    void MyClass::Func()
    {
       if (m_flag)
       {
            Foo();
       }
    }
    
    void MyClass::Foo()
    {
        if (m_flag)
        {
             ...
        }
    }
    Guess I was just trying to make sure that m_flag was indeed set before executing Foo(). Thankfully a co-worker caught it in a review and I changed it. He asked if my second check was just a sanity check or what. I told him it was more like an insane check.
    It's actually not that bad to do the check twice if there were performance concerns about the overhead of a function call when it wasn't strictly needed, or if Foo's design specs didn't explicitly require it to check m_flag and skip the body of the function if false -- even if that's how Foo currently works, my opinion is that if it's not in writing, somebody's going to change it in the future and break my code just because the universe hates me like that.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    The first check would ensure that Foo() would never get called if m_flag was set so if Foo() did get called then obviously m_flag was set so there is no need to check it...performance or not. Since there were no threads in the system there was no way that m_flag could get unset before calling Foo().

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by Bubba View Post
    The first check would ensure that Foo() would never get called if m_flag was set so if Foo() did get called then obviously m_flag was set so there is no need to check it...performance or not. Since there were no threads in the system there was no way that m_flag could get unset before calling Foo().
    Well, that assumes that no other code will ever call Foo() except Func(), now or later. Of the two statements, the one I'd consider optional -- assuming Foo()'s body requires m_flag be set -- would be the first, not the second. That way, ten years down the road when someone decides they need a function just like Foo and decide to be clever and just call the existing one, then it'll work as expected; it doesn't force the caller to verify the flag.

    Of course which if() you can omit depends a bit on logically whether m_flag should be checked in Func() or Foo(); that would depend a lot on exactly what the flag was signaling. But since it's a member-level variable and not a local variable to Func(), it seems more likely to me that the proper place to check is within Foo().
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    I'll have to digup my own example, but on the topic of comparing with zero, I don't see why you shouldn't. Zero is zero -- exactly. If it wasn't, then you could multiply something with "zero" and get a non-zero result. That basically breaks all of mathematics. Comparing for exact equality with floats is dubious, but zero is a special case. Zero is zero.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    These are actual pieces of code I wrote one time or another. I collect them when I realize what I did. I think I mentioned them before in the past...

    This one was done as a part of a log system and analysis tool I was implementing for my Mediaeval pbem game server. Anal being a class that describes those parts of the execution I want to profile. I did change the name eventually.
    Code:
    Anal performance;
    Assembly specialized classes are ripe for abuse. This is the main class (and object) responsible for holding the z80 instruction set in my never finished z80 emulator.
    Code:
    //Assembly
    Assembly myAss;
    This is just part of a code to manage plugins that I since rewrote entirely (aka this does not exist anymore). I remember feeling kinda sad for it not being private.
    Code:
    int Assets::Show();
    And...
    Code:
    double stop; // and against the -wall
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Bubba View Post
    The first check would ensure that Foo() would never get called if m_flag was set so if Foo() did get called then obviously m_flag was set so there is no need to check it...performance or not. Since there were no threads in the system there was no way that m_flag could get unset before calling Foo().
    It's called defensive programming. The biggest problem comes from testing, that is the condition of the supplying method will only get called by methods that acknowledge its error checking. You can imagine the bugs with 50,000 scattered calls, and legacy code (with some wrong conditions).

    Yet another reason I love design-by-contract

  11. #11
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    974

  12. #12
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    //Assembly
    Assembly myAss;
    Haha thats legendary
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  13. #13
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    Quote Originally Posted by Yarin View Post
    Sorry, I just don't get it. I know what your doing, but I don't see what's so funny. I also don't check floats for 0.0 - but I still don't get the 'humor'.

    Is it because, in theory, the event wouldn't even be called in the first place if the event was zero?
    Sorry Yarin, the humor in my piece of code is in the variable name I choose: changeWeCanBelieveIn.

    Since I couldn't really trust the value in the variable ExtentWidthChange, I made a new variable "changeWeCanBelieveIn", which is the more "trustworthy" value that I really want.
    My Website

    "Circular logic is good because it is."

  14. #14
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I was thinking of a name of an object that really just needs to exist as lock requires in C#. So after wasting a minute for a name I thought "just put the first thing that comes in mind"
    Code:
    lock (theDoor)
    {
     ...
    }
    Quote Originally Posted by Mario F.
    //Assembly
    Assembly myAss;
    Did the same thing a couple months ago...

  15. #15
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Imagine having a system that supports multi-head video cards. One needs to give access to each head so they can draw via that head.

    Function name we chose:

    Device::GetHead()

    Didn't really hit home until the code review and needless to say after the review that day wasn't very productive.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21