The Foo, the Bar, the Ugly

johannesvollmer

Johannes Vollmer

Posted on January 31, 2018

The Foo, the Bar, the Ugly

Image source: cinetropolis.net

When I learned my first programming language, Java, I just threw my questions into google. The web™ offers tons of helpful guides, and of course we all know StackOverflow, but theres a minor problem with many of them.

Code Examples

Examples are undeniably one of the most effective way of learning. They are essential for any programming tip, tutorial, or guide. Glady, the majority of guides do have them.

Specifically: Bad Code Examples

Unfortunately, someone once decided it was a good idea to do code examples using completely fictional identifiers without any meaning; the good, the bad, the ugly: Foo, Bar, and Baz.

They have been used to name entities such as variables, functions, and commands whose exact identity is unimportant and serve only to demonstrate a concept. Wikipedia

Why are these words bad?

Let's have a look at an example of (an example using these keywords). OMG so meta!
This example demonstrates how interfaces in Java work:

interface Foo {
    void foo();
}

class Bar implements Foo {
    @Override 
    public void foo(){
        System.out.println("Bar");
    }
}

class Baz implements Foo {
    @Override 
    public void foo(){
        System.out.println("Baz");
    }    
}

// ... later
void baz(Foo foo){
    foo.foo();
}
Enter fullscreen mode Exit fullscreen mode

Admit it: You didn't really read this example. It's just not fun to read. Relations between classes, interfaces and methods can only be learned by carefully reading the source code, maybe even only when already knowing a little bit about interfaces.

A key property of examples is that they can be related to. Thus, choosing a fictional and meaningless word contradicts the very definition of 'example'. By using Foo, you fail to provide context.

What should we do instead?

Wouldn't this example of interfaces be much easier to understand:

interface SomethingThatPurrs {
    void purr();
}

class Cat implements SomethingThatPurrs {
    @Override 
    public void purr(){
        System.out.println("*purring cat*");
    }
}

class Kitten implements SomethingThatPurrs {
    @Override 
    public void purr(){
        System.out.println("*purring kitten*");
    }    
}

// ... later
void makeThatThingPurrSomehow(SomethingThatPurrs purring){
    purring.purr();
}
Enter fullscreen mode Exit fullscreen mode

Cats and Kittens create a context. It's suddenly very easy to see that both of them can purr, and how SomethinThatPurrs should somehow relate to both. That's because you know that cats and kittens both purr.

I've seen beginners wondering what Foo and Bar actually mean, until someone tells them that there is no hidden meaning with that. Some even think that Foo and Bar are funny, which in turn motivates teachers to construct examples using these names.

In my opinion, when trying to explain a concept, one should do all that can be done in order to help the learnee. Providing context by using specific names and well-known concepts helps a lot.

Conclusion

  • Foo and Bar do not provide context
  • Examples should provide as much context as possible
  • Foo and Bar are uncreative and boring
  • Kittens are cute and inspiring

Foo and Bar are said to origin in the military acronym "FUBAR", which expands to "Fucked up beyond all recognition". What a coincidence.

💖 💪 🙅 🚩
johannesvollmer
Johannes Vollmer

Posted on January 31, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

The Foo, the Bar, the Ugly
mentoring The Foo, the Bar, the Ugly

January 31, 2018