Search Unity

Hilarious Code

Discussion in 'General Discussion' started by Dustin-Horne, Dec 23, 2015.

  1. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,562
    Thought I'd start something a little fun. Is there any hilarious code you've found when working on projects? This could be code you did in the past or someone else. One rule, no finger pointing / naming actual people, just funny code.

    Here is a snippet of code I found while doing a code review for an offshore team we are working with. In fairness they are almost all junior developers. What makes the code below so funny is that vendorId is an "int".

    doh.PNG
     
    TenKHoursDev and jpthek9 like this.
  2. AndrewGrayGames

    AndrewGrayGames

    Joined:
    Nov 19, 2009
    Posts:
    3,823
    Have you found The Daily WTF?

    My first mentor showed that site to me as an instructional aid when I was a new developer, the point being, just because you can do something in code, does not mean you should.

    There's some good ones in there.
     
  3. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,473

    Wish I had some to share but that is really funny lmao.
     
    Dustin-Horne and AndrewGrayGames like this.
  4. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,562
    I have, it's a great site. I used to get Visual Studio Magazine as well before they stopped sending it out in print and they had a section called Dev Disasters that was great.

    Another one I found that they did was a Dictionary that used a Dictionary as a key, so it was like:

    Dictionary<Dictionary<string, int>, string>

    It was mind boggling.
     
    AndrewGrayGames and CDDelta like this.
  5. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,473
    *Face Palms* lmao lol.

    I always thought coding was pretty straight forward. I mean obviously when I first started learning to code it took me a little bit, but after about a month or so I caught on to how repetitive everything is, I found quickly I'm almost always using the same things over and over again. float, int, list, bool, and so on.
     
  6. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    6,434
    You know, unless I overlooked something...

    Since Dictionary doesn't appear to overload operator==, it is a reference type AND (IIRC) default behavior of operator== in C# is to compare references for equality without calling Equals, this essentially is associating a string with random dictionary instance.

    Now the trick question is if that's what the dev actually wanted to do.
     
    AndrewGrayGames likes this.
  7. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    6,434
    The "funniest" thing I can remember at the moment was when someone wrote a C++ dll returning std::string and built it using different compiler.

    It gets more interesting when you get to high level OOP phase in language like C++.

    Project development goes in cycles: you get a task, you make set of abstractions for it, then task changes into something that is incompatbile with already established OOP model, so you need to fix that without breaking the program and without rewriting half of it.

    Sometimes it can be like rebuilding bottom floor of 2 meter tall card house during the earthquake while someone is dancing on top of the card house.
     
  8. Prototypetheta

    Prototypetheta

    Joined:
    May 7, 2015
    Posts:
    119
    I'm afraid I don't have it on me, but for some of my... Less stable physics applications, I employ what I call the "F*** it" function. When the object detects that something has gone wrong, instead of trying to rectify that it simple prints out "F*** it" in the console and destroys the game object.

    Not really hilarious code so to speak, more a mildly amusing if someone childish way to tell
    you that your code is S***.
     
  9. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,562
    You're correct. So in order to resolve the value from the outer dictionary, someone would need to pass the inner dictionary back into it as the key. What made this really awful is that it was a return value for a business logic method, so the only way you ever have those keys is by extracting them from the outer dictionary in the first place. It was a lazy attempt by the developer to return multiple pieces of data without using a proper business object model.
     
  10. AndrewGrayGames

    AndrewGrayGames

    Joined:
    Nov 19, 2009
    Posts:
    3,823
    I can see it now...public static void F×××It ()...
     
    Dustin-Horne and Philip-Rowlands like this.
  11. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    6,434
    I see.

    That structure would make sense if someone just stored many dictionaries for "bookkeeping" or whatever, but as return value this is definitely not gonna fly.
     
    Dustin-Horne likes this.
  12. jpthek9

    jpthek9

    Joined:
    Nov 28, 2013
    Posts:
    944
    In some countrieis, zero is the same thing as null (it's nothing). They might be trying to say `if (vendorID <= 0)`.
     
    darkhog likes this.
  13. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,562
    Is that the case in India? And yeah, there were trying to say <= 0. In Entity Framework 0 and Null are the same thing for an identity column, of which VendorId is, but this was a data retrieval method. What they were attempting to do is do a quick sanity check on the id and not query the database if it was <= 0.

    What I actually found must shocking is the fact that int is a non-nullable type, but the compiler actually compiles it! There must be some kind of implicit conversion in those operators, and it only results in a compiler warning and not an error.
     
  14. Philip-Rowlands

    Philip-Rowlands

    Joined:
    May 13, 2013
    Posts:
    336
    One of my colleagues at work mentioned over lunch recently that his debugging code at university was literally print statements with "this section's f***ed up", "you divided by zero you bollix", etc. He ended up writing a Python script that would trawl through his code and remove all print statements before handing it up :D

    EDIT: Another one from work. I was setting up GruntJS to call UglifyJS to minify a file. One of the options for UglifyJs is "screw IE8" - which means "I don't care about full compliance with quirks in IE6-8.
     
    Last edited: Dec 23, 2015
  15. Neoptolemus

    Neoptolemus

    Joined:
    Jul 5, 2014
    Posts:
    52
    I had a real face-to-keyboard moment at work a couple of weeks ago. We discovered that running the client's automated financial system was generating huge transaction logs in the database (like 6+ gb).

    Had a quick look, turns out some genius was writing a query that inserted 11m rows (fair enough), but was then updating the entire table to set a column to be a constant, THEN running a delete to remove rows where an amount was 0. After the delete, the number of rows was something like 4000.

    Why didn't they just put the constant in the insert, and a where clause so the 0's were never inserted to begin with? No idea, it was a fairly complex query so maybe they panicked. It took me 30 seconds to fix.

    Bonus points for performing the delete AFTER the update.
     
    Dustin-Horne likes this.
  16. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,006
    FuzzyQuills, goat, Kiwasi and 2 others like this.
  17. Philip-Rowlands

    Philip-Rowlands

    Joined:
    May 13, 2013
    Posts:
    336
    *Starts wiping tears of laughter from his face*

    total_hours_wasted_here...I have to create an Editor script for that.
     
    FuzzyQuills, goat and AndrewGrayGames like this.
  18. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,452
    I did something similar once.

    Code (CSharp):
    1. try {
    2.     myFloat = data[index];
    3. } catch {
    4.     Destroy(gameObject);
    5. }
    The code worked fine, and even made sense when I wrote it.

    Then months later I changed the way the indexer worked on data. And objects just started randomly disappearing. Took me hours to track it back to this particular script.
     
    landon912 likes this.
  19. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,572
    Took me a while, but I dug this gem up.

    Wat
     
    RockoDyne likes this.
  20. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,316
    Half the stuff I download from the asset store has WTF code.
     
    Socrates and AndrewGrayGames like this.
  21. Bradamante

    Bradamante

    Joined:
    Sep 27, 2012
    Posts:
    293
    With regards to using a dict as a dict key. I don't know the behavior in C#, but in Python this is feasable. Dicts are hashable, dict keys are hashable ... so ... yeah. I don't know the intricacies though.

    Maybe they used the nullable variant of int, i.e.

    Code (csharp):
    1. int? myNullableInt = 1;
     
    AndrewGrayGames likes this.
  22. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,562
    In this case it would hash the pointer since it's a reference type.

    Unfortunately not. :)
     
  23. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    377
    Senior reviewed some guys code (we called him Chongo) way back when and told him to clean up the warnings. So, Chongo adds compiler command to not show warnings. Senior almost lost his mind. Oh, almost forgot, he also would create objects pass them to a method and recreate them. He was probably making more than (~16 years ago) me back then too, sniffle.

    Oh, that reminds me also of the time I was contracting and this other guy was getting $15 (edit, actually it was $25 more an hour, me brain no work good no mo) more an hour than me. One day he asks me to help him debug something... I had to show him how to set a breakpoint... wha wha
     
    Last edited: Dec 24, 2015
  24. GarBenjamin

    GarBenjamin

    Joined:
    Dec 26, 2013
    Posts:
    7,423
    Well that makes sense. Once you get to a certain level your mind is on other things. You don't have time to mess around with the little things and all that is important is to know how to find out the little things when you need them.

    There are three fairly common quotes along these lines:

    I don’t need to know everything; I just need to know where to find it, when I need it.
    Never keep anything in your mind that you can look up.
    Never memorize what you can look up in books.


    So in your example they were just making use of their available resource... you. :)
     
    Dustin-Horne, tango209 and Ryiah like this.
  25. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,869