Why Do Programmers Use “foo” and “bar”? What Does It Mean?

If you’re an up-and-coming programmer or have buddies who are programmers, you’ve probably either seen or heard the terms ‘foo’ and ‘bar’ being tossed around every once in a while. What exactly do these terms mean, and why do programmers use them?

Programmers use “foo” and “bar” as common metasyntactic variables. Metasyntactic variables are fundamentally placeholder variables that are utilized in coding. They are used in place of proper variables, and it is intended for them to be updated, changed, or deleted at a later stage of programming.

The remainder of this article will explore in further depth the terms “foo” and “bar,” their origin and use, and will also explain why they might ring a bell in your mind. Keep reading to find out more.

“Foo” and “Bar” Are Common Metasyntactic Variables

As explained earlier, “foo” and “bar” are common metasyntactic variables. In fact, “foo” is the most common metasyntactic variable out of them all.

Now, the words ‘metasyntactic’ variable might appear intimidating at first, but all you need to know for a basic understanding of what they mean is that they refer to terms that are common placeholders used while writing code.

Why would programmers need to use placeholder terms while writing code? Well, a major part of it is that they don’t have to spend time coining up a more accurate term for the variable if there is no need for the variable to have a unique and accurate name.

This can be the case if, for example, a programmer needs to write up a short code simply for the purposes of showcasing or demonstration.

The goal in this scenario would be to illustrate the code’s core processes and show others how it works – the name of the variable involved itself would likely make little to no difference. Using “foo,” “bar,” or both, in this case, would be perfectly acceptable.

You may have come across “foo” and “bar” – or both – used together very frequently in online coding tutorials. This frequent usage is because these tutorials want to emphasize the fact that the names of the variable, in this case, are unimportant, arbitrary, and subject to change. The code that follows is what deserves your focus and attention.

Programmers can also use metasyntactic variables in the earlier stages of programming. By giving variables an arbitrary name, programmers can focus on writing the coding and optimizing the inner workings of the code instead of bothering with the naming of the variables.

The names of the variables can be adapted to fit the program accordingly at a more appropriate time.

“Foo” and “bar” can be used if the name of the variable they will hold is of little consequence or if there is simply not enough information available to the programmers to decide on a dedicated name for the variable. In this case, “foo” and “bar” are quite convenient to default to.

Other metasyntactic variables include:

  • Baz
  • Qux
  • Quuz
  • Quux
  • Corge

There are also metasyntactic variables in other languages. “Hoge” and “fuga” are two of the more popular ones in Japanese.

The Origin of “Foobar”

It appears that “foo” and “bar” started gaining popularity in coding sometime around 1960-1970. The terms were popularized by MIT and DEC around this time.

However, it should be noted that “foo” had already been a somewhat known ‘nonsense’ word before this time, which might have made it an ideal candidate to be used as a placeholder word in coding.

This thread on stack overflow will provide you with an in-depth, reference-based exploration into the origin of the terms “foo” and “bar” if you wish to find out more.

“Foobar” and “FUBAR”

I mentioned earlier that when spelled together (as they often are), the words “foo” and “bar, or “foobar” might ring a bell. Well, this is probably because of how similar they sound to the better-known term “FUBAR.”

To be clear, they are entirely different. While many speculate that the words “foo” and “bar” originated from “FUBAR,” the two mean different things.

“FUBAR” is a term that was commonly used by the U.S military during the periods of the second world war. It stands for “f***ed up beyond repair” or “f***ed up beyond recognition.” In contrast, “foobar” doesn’t really mean anything without the context of being used while writing code.

As for “FUBAR” being the origin of “foobar,” there is definitely some evidence for this being the case. Especially when you consider how they are pronounced almost the same, and how “foo” and “bar” are terms used together more often than not.

Regardless of whether “FUBAR” was the origin of “foobar” or not, it can be said with a high degree of certainty that the term “FUBAR” was at least somewhat responsible for promoting “foobar” in terms of popularity.

Why “Foo” and “Bar” Are Used

The reason for this is quite simple, to be honest. It is because they just so happened to be picked up by programmers and coders. They gained a great deal of popularity as metasyntactic variables, and their use in coding skyrocketed.

While numerous other metasyntactic terms could serve the purpose of being a placeholder just as well, it is the reality that using “foo” and “bar” or “foobar” is now a convention and standard practice in coding.

Since these terms are placeholders, one of their primary requirements is to be easily recognizable as such by other programmers who happen to come across them in writing or hearing.

“Foo” and “bar” are the most well-recognized among all other placeholder terms. “Foobar” as a term is so popular, in fact, that there is a dedicated audio player for windows named ‘foobar2000’.

Using lesser-known terms might end up causing confusion among readers because usually, in coding, variable names have to be accurate and suitable. Understanding code is hard enough as it is, right?

For this reason, sticking to convention becomes important, and using “foo” and “bar” becomes favorable over using the other, lesser-known terms that programmers might not have as easy a time picking up on.

There are occasional use cases for other metasyntactic variables. If there are more than two variables that need placeholder names, it is common to follow up “foo” and “bar” with “baz” and “qux.”

However, know that it is rarely the case that such a situation arises. Even rarer would be code that involves more than four placeholder variables.

In such a situation, it would probably be best for the programmer to give the variables actual names instead of continuing to use placeholder variables, as the end code would become quite confusing to read and understand.

Final Thoughts

“Foo” and “bar” are two of the most commonly used metasyntactic variables. These are terms used as placeholder names for variables in coding and programming. They don’t really mean anything as words and are just used in place of a more accurate name, generally for purposes of convenience.

They were made popular during the 1960s and ’70s by MIT and DEC, and the military term “FUBAR,” while meaning something entirely different, likely contributed greatly to their popularity.

While there are a lot of other metasyntactic variables, “foo” and “bar” are used most commonly as they are well-known and well-understood.

Was this article helpful?

Share your thoughts by replying on Twitter of Become A Better Programmer or to personal my Twitter account.