Developer Insight Series, Part 4: Favorite and Funny Code

By Janice J. Heiss, June 2009    

Over the years I've heard noted developers talk about their favorite code, funniest code, most beautiful code, how to write code, how not to write code, the obstacles to writing good code, what they love and hate about writing code, and so on. In the process, I've encountered a lot of insight that is worth preserving--and heard some funny stories.

Parts One and Two of this series provided advice on how to write good code. In Part Three, developers reflect on the actual process of writing code, how it happens, what it feels like, and how they do it.

In the fourth and final part of the series, three developers share their funniest and most favorite code, and tell funny stories.

Contents
 
Joshua Bloch: Here's a Mystic Code Poem
Tom Ball: CT Forever!
Masood Mortazavi: The Interviewers Didn't Believe I'd Written the Program
See Also
Comments
 
Joshua Bloch: Here's a Mystic Code Poem
Joshua Bloch
Joshua Bloch
 

Chief Java architect at Google, Joshua Bloch is well known as the author of Effective Java (now in its second edition) and Java Puzzlers (with Neal Gafter). In a former life as a senior staff engineer at Sun Microsystems, and an architect in the Core Java Platform Group, he designed and implemented the award-winning Java Collections Framework, the java.math package, and contributed to many other parts of the platform.

He holds a Ph.D. in computer science from Carnegie-Mellon University, where during the defense of his dissertation, which was open to the public, he planted a long technical question with his mother that he answered flawlessly after saying, "Awww, Mom!" He responded to another planted question with a rap song, backed by a recorded rhythm track played on a boom box concealed under the desk.

Like real poetry, a piece of code that's beautiful to someone might be ugly to someone else. Here's a mystic code poem:

 
    static int inverse(int val) {
        t *= 2 - val * t;
        t *= 2 - val * t;
        t *= 2 - val * t;
        t *= 2 - val * t;
        return t;
    }
 

As its name implies, this method calculates the multiplicative inverse of its odd argument, mod 2ˆ32. In other words, for all odd integers i, i * inverse(i) == 1. It works by Newton iteration.

There's an elegant proof that the four iterations--the unrolled loop in the method--is sufficient for any input value. Is this the most beautiful code I've ever seen? No, but it's darn cute.

If you like this sort of thing, you should see Henry S. Warren's book Hacker's Delight (Addison-Wesley, 2003). While you're at it, check out Henry's chapter on "The Quest for an Accelerated Population Count," in Osram and Wilson's Beautiful Code (O'Reilly, 2007). And while you have the book in your hands, also read Jon Bentley's chapter on "The Most Beautiful Code I Never Wrote." Jon writes a lot of poetic code.

What's the funniest code you can think of?

The Daily WTF is full of funny code. Also Bill Pugh often sends me funny code that FindBugs uncovers. Here's a delightful example--un-retouched, I swear:

   public Object getObject(java.util.Map<String,Class<?>> map)
       throws SerialException
   {
       map = new Hashtable(map);
       if (!object.equals(null)) {
           return map.get(object);
       } else {
           throw new SerialException("The object is not set");
       }
   }
 

Can you give us an example of code that you are most proud of creating and explain why?

The Collections framework. It's far from perfect, but it's proven itself maintainable and pleasant over the years. Doug Lea built many parts of java.util.concurrent atop it. And I still get letters from programmers telling me how much more pleasant it makes their jobs. It lets you write stuff like this little program, which computes all the anagrams in the file on standard input:

public class Anagram {
    public static void main(String[] args) {
        int minGroupSize = Integer.parseInt(args[0]);
 
        // Read words from input and put into simulated multimap
        Map<String, List<String>> anagrams =
            new HashMap<String, List<String>>();
        for (Scanner s = new Scanner(System.in); s.hasNext(); ) {
            String word = s.next();
            String alphagram = alphagram(word);
            List<String> group = anagrams.get(alphagram);
            if (group == null)
                anagrams.put(alphagram, group = new ArrayList<String>());
            group.add(word);
        }

        // Print all permutation groups above size threshold
        for (List<String> group : anagrams.values())
            if (group.size() >= minGroupSize)
                System.out.println(group.size() + ": " + group);
    }

    private static String alphagram(String s) {
        char[] chars = s.toCharArray();
        Arrays.sort(chars);
        return String.valueOf(chars);
    }
}
 

Read the full interview with Joshua Bloch.

Tom Ball: CT Forever!
Tom Ball
Tom Ball
 

Tom Ball is a software engineer at Google, working on Java development tools. He began working with Java in 1994 as part of Sun's JDK, AWT, Swing, Jackpot, and NetBeans teams, and contributed to the Java FX Script compiler team. He has no formal university education.

What's the funniest thing that has happened to you as a developer?

Well, my sense of humor is skewed, but here goes. When Convergent Technologies (CT) was acquired by Unisys, the engineering team that "enhanced" our OEM product finally got their revenge after years of getting beaten every time they tried to out-engineer us: They became our management. Consequently, most of our star players left, and morale bottomed out. Because I owned the editor, as a small act of defiance I added an Easter egg to it, which, when triggered by a certain key sequence, would add "CT forever!" to the displayed version string.

This brightened everyone's mood, and when the Unisys developers discovered it, they appreciated it for its team-building value. But then a pointy-haired manager caught wind of it and filed a P1 demanding that it be removed. I closed it and suggested that one of their engineers "fix" the issue because they had all the source code. The Unisys engineers studied the source for weeks, while the manager demanded daily updates on the status of the so-called keyboard bug. Finally, my director politely asked if I would fix the keyboard bug because their engineers had given up. I did this and then sent an email to everyone saying that I wasn't trying to inflame tensions between our teams, but that "CT stood for engineering excellence, and so I hope that whenever people search for CT in our products, they'll find it." Case closed.

To this day, if you bring up the editor's search-text dialog and enter CT, "CT forever!" shows up in the version string. It's just not triggered by the keyboard anymore.

Read the full interview with Tom Ball.

Masood Mortazavi: The Interviewers Didn't Believe I'd Written the Program
Masood Mortazavi
Masood Mortazavi
 

Masood Mortazavi manages a team of senior database engineers at Sun who contribute to several open-source communities: Java DB (Apache Derby), PostgreSQL and MySQL, and PHP. He started at Sun in 1998 with the J2EE reference implementation team, worked on continuous availability problems, and participated in building technical relationships with business partners among telecommunications network equipment providers. He holds a Ph.D. in computational fluid dynamics, along with advanced degrees in business and journalism. In addition, he spent several years studying the logic and methodology of science.

Back in the mid-1990s, an Internet search company interviewed me for a job and asked me to reproduce, on the white board, a Java program I had written to implement a set of path-planning algorithms. I had posted the program, as an applet, on my web site at the Mathematics Department at Berkeley. Among other features, it produced a navigation "plan" (and could demonstrate all intermediate plans visually) through a set of user- defined geometric obstacles in space. It used A* search and some geometric algorithms to accomplish this task, and it had a user interface in which the user could define the obstacles and start the planning engine.

The interviewers didn't believe that I'd written the program myself. What I wrote on the white board didn't encourage them to believe otherwise, or appear to make them feel any easier. While the people who'd invited me to the interview were really excited about the program, I was literally shooed out of their offices by the CTO. I was dumbfounded and didn't know what to make of it, and it became a funny story I would later tell my friends. The story reflected the classic economic problem of asymmetric information in the concrete case of assessing people's skills before they have started working in a team. (I'm assuming the CTO was not shooing me away for some political reason.)

Read the full interview with Masood Mortazavi.

See Also

The Developer Insight Series, Part 1: Write Dumb Code: Advice from Four Leading Java Developers
The Developer Insight Series, Part 2: Code Talk
The Developer Insight Series, Part 3: The Experience of Writing Code
Joshua Bloch Interview
Tom Ball Interview
Masood Mortazavi Interview and Blog
Top Java Developers Offer Advice to Students

Rate This Article
 
 
Comments

Do you have amusing code or a funny software story to share? We'd love to see it. We welcome your participation in our community. Please keep your comments civil and on point. You may optionally provide your email address to be notified of replies--your information is not used for any other purpose. By submitting a comment, you agree to these Terms of Use.