DEV Community

Cover image for 16 Best Source Code Gems [Collection]
Lorenzo Pasqualis
Lorenzo Pasqualis

Posted on • Updated on • Originally published at coderhood.com

16 Best Source Code Gems [Collection]

This post was first published on CoderHood as 16 Best Source Code Gems [Collection]. CoderHood is a blog dedicated to the human dimension of software engineering.

Developer's personalities shine in odd ways. One them is in a (sometimes) subtle sense of humor, choice of words and attitude that you can find in code, code comments, and error messages. It often feels like a stream of consciousness that reflects the human reality of a moment in time.

I collect those gems and here I am sharing with you some of my favorites. Some I found myself in the code I worked with. Others, I collected from various sources. All have been stripped of anything that could be considered IP.

1 -- Wild Compiler.

  /*
   * I don't know why the compiler hates me.
   * I changed this comment many times, and every
   * time it generated the same executable.
   * You should try. It's wild.
   */

Enter fullscreen mode Exit fullscreen mode

2 -- If You Get Here.

  /*
   * If you get here, I screwed up.
   * If I screwed up, I clearly don't know what I am doing.
   * If I don't know what I am doing, and you are looking at my code...
   *   ...we are both in trouble.
   */
Enter fullscreen mode Exit fullscreen mode

3 -- Classic, Useless Documentation.

  int  i  =  0;  // Set i to zero.
Enter fullscreen mode Exit fullscreen mode

4 -- Whatever You Do, I Can Do Better.

  int  getRandomNumber()  {
      return(rand()+1);  // You have been one-upped
  }
Enter fullscreen mode Exit fullscreen mode

5 -- Don't Even Try.

  /*
   * Don't even try to understand this.
   * I wrote it, and I have no idea why it works.
   * But it does. My subconscious is that good.
   */
Enter fullscreen mode Exit fullscreen mode

6 -- Feeling Sick.

 Exception up  =  new  Exception("Something is really wrong.");
 throw  up;Â Â // ha ha
Enter fullscreen mode Exit fullscreen mode

7 -- CUI (Coding Under the Influence).

 // drunk, fix later
Enter fullscreen mode Exit fullscreen mode

8 -- No Comment

   /*
    * Sorry, I have removed all comments from this code.
    * If you can't understand it, you shoudn't touch it anyway.
    */
Enter fullscreen mode Exit fullscreen mode

9 -- Sincerely

 public  boolean  thisCodeSucks()  {
      return  true;  // sincerely
 }
Enter fullscreen mode Exit fullscreen mode

10 -- Random Number

    int  getRandomNumber()  {
      return  4;  // Guaranteed to be random. I used a dice.
    }
Enter fullscreen mode Exit fullscreen mode

11 -- So Long

Not code, but still really funny.

12 -- Human Needs

This is taken from the Apple Chess Engine code.

  //
  // Paradoxically enough, moving as quickly as possible is
  // not necessarily desirable. Users tend to get frustrated
  // once they realize how little time their Mac really spends
  // to crush them at low levels. In the interest of promoting
  // harmonious Human - Machine relations, we enforce minimum
  // response times.
  //

  const  NSTimeInterval kInteractiveDelay    =  2.0;
  const  NSTimeInterval kAutomaticDelay  =  4.0;

Enter fullscreen mode Exit fullscreen mode

13 -- Textbook Comment

  /*
   * Explanation omitted as an exercise for the reader.
   */
Enter fullscreen mode Exit fullscreen mode

14 -- Expiring Code

 // If you are reading this after 1/1/2010
 // there's something wrong.
 // This should have been ripped out by then.
 if  (DateTime.Now  >  new  DateTime(2010,  1,  1))  {
     throw  new  Exception("This code is now obsolete");
 }
Enter fullscreen mode Exit fullscreen mode

15 -- "Temporary" Code

 // somedev1 -Â Â 6/7/02 Adding temporary tracking of Login screen
 // somedev2 -Â Â 5/22/07 Temporary my ass
Enter fullscreen mode Exit fullscreen mode

16 -- Family Issues


/*
 * The game of life
 */
 if  (status==SINGLE)  {
      money  =  1;
 }  elsif  (status==DIVORCING)  {
      money  =  money  /  2;
 }  elsif  (status==DIVORCED)  {
      money  =  money *  2;
 }  else  {  // status==MARRIED
      money  =  rand()  *  monthly_salary  +  0;  // Savings is represented by zero.
 }
Enter fullscreen mode Exit fullscreen mode

If you enjoyed this article, keep in touch!

Discussion (13)

Collapse
davidhaile profile image
David Haile

Good! #2 If You Get Here...

Which reminds me: I inherited a pile of crap code that actually worked fairly well. The original programmer (forced into it) didn't know how to use a switch() statement and what he implemented was a complicated state machine with if () else if () else... nested very, very deep! The nested if,if,if statements got soo deep that at one point I added a comment that if the code reached that location the engine must be on fire (it was a generator controller).

A couple of years later they resurrected the project and of course reviewed the code and found my comment. I got in a lot of hot water over that one. My recommendation for others who find themselves in a similar position of inheriting crap code, go ahead and make comments like that, but somewhere in the header document that you inherited it from and make dam sure that future reviewers know your story. Reviewers will or should learn that is under suspicion.

Collapse
lschultebraucks profile image
Lasse Schultebraucks

Haha, really nice 😁

Collapse
maxart2501 profile image
Massimo Artizzu • Edited

#10 Someone actually reads xkcd: xkcd.com/221/

xkcd 221

Collapse
peterchaula profile image
Peter Chaula

It's also a pure function

Collapse
sabakumoff profile image
Sergey Abakumoff

some more funny comments : medium.com/@sAbakumoff/157-million...

Collapse
damcosset profile image
Damien Cosset

Wow, some of those are amazing :D

Is it weird if I also feel a bit scared that developers actually have to deal with that kind of work?

Collapse
lpasqualis profile image
Lorenzo Pasqualis Author

LOL. Not weird at all. Some scare me too :)

I am convinced that there is a whole bunch of things like that that never surface (especially in companies where individual coder ownership is normal).

Collapse
polentino911 profile image
Diego Casella

// Trust me, I'm an engineer

Collapse
lennartb profile image
Lennart

A lot of them from here: stackoverflow.com/questions/184618...

Collapse
dutrius profile image
Dylan Towers

One of the favorite ones I've seen:
this.parent = null; //Become Batman

Collapse
lobsterpants66 profile image
Chris Shepherd

Christmas eve 2012 I was asked to look at some Excel VBA code I had written many years previously. This comment was at the top of the code:

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' SendTrades

' ==================
' Date : 24/12/2002 - MERRY BASTARD CHRISTMAS!
' Author : Chris Shepherd
' Description : Send trades to Bloomberg
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

10 years later i was back doing the same thing. This led to my coding standard being "keep it simple enough to be able to understand it on Christmas Eve with a terrible hangover".

Collapse
inozex profile image
Tiago Marques

I had a code that was exactly number 5... I did write... but I didn't really understand all the flow... But the fact was that worked! So... I guess my "subconscious is that good" 🤣🤣🤣🤣😂😅

Collapse
twof profile image
Alex Reilly • Edited

Some of my favorite source code from the past week
NightMiner