Death to FooBar

· Sam Ireland

When reading programming tutorials, or reading examples of code demonstrating something, or really reading anything about some new concept in programming - you are likely to come across certain words over and over again: 'Foo' and 'Bar'.

They are used as 'placeholder names' in example code when the name of something doesn't matter. So, for example, if you were writing code that showed how functions work in Python, you might put something like this:

def foo(bar, baz):
    return bar + baz

Here you are showing how to make a function called 'foo' which takes two arguments, 'bar' and 'baz' (another placeholder name), adds them together, and returns the resultant sum. Code like this can be found all over websites like StackOverflow.

Well, I think this is terrible, and unhelpful, and should be stopped, and here's why. Look again at that code above. If you already know what that code does, and how functions work in Python, it probably looks fine. But if you don't know how functions work in Python, or even what they are - and, presumably, that is the target audience which such demonstrations - there are so many needless avenues for confusion here. If you don't know that 'foo' and 'bar' are basically programming in-jokes, how do you know that those names don't matter? The word `def` at the start is very much not a random placeholder word, but to the untrained eye there is no reason to pick that three letter word as the crucial one out of the four gibberish three letter words on that line.

For a learner, is the following not vastly more obvious:

def function_name(argument1, argument2):
    return argument1 + argument2

...or even...

def add_two_numbers(number1, number2):
    return number1 + number2

Here it is obvious what the function name is, because it is a description of what it does, which obviously isn't going to be a built-in Python keyword. It's obvious what arguments are and how they work - or at least more so than it was originally.

It can be difficult, once you have been doing this for a while, to forget what a beginner will and won't know. That it won't be obvious that 'foo' and 'bar' are little in-jokes. Because ultimately that's what they are - they are a signal to other experienced programmers that they know this little code, which benefits the beginner very little.

Many people do get this, of course - FooBar isn't ubiquitous and many people do write very helpful, descriptive example code. But we have nothing to lose and everything to gain by just dropping the FooBar convention for good. The next generation of programmers will thank us for it.