Why do programmers use foo and bar?

Open enough programming tutorials and two characters keep returning. There is
a function called foo, a value called bar, and, when the example needs one
more name, a baz waiting nearby.
The words do not describe the data. That is their job. They tell the reader, “this name could be anything; pay attention to the structure around it.” But programmers did not invent them from nothing, and the familiar explanation that foobar is simply a cleaned-up military acronym leaves out most of the story.
The trail passes through a surreal newspaper comic, an MIT model railroad club, early hacker culture and the documents that built the internet.
A name that means no particular name
The technical label for foo is a metasyntactic variable: a placeholder
used in the language we use to talk about programs. If a tutorial says that
foo() calls bar(), it is not telling you what either function does. It is
showing a relationship between two functions without letting realistic names
distract from the point.
The Jargon File’s definition
calls foo the canonical example and records a loose standard sequence:
foo, bar, baz, qux, quux. Different communities developed their own
sets. Python documentation famously reaches for spam and eggs; British
examples may use ordinary names such as fred. The choice became a small
cultural signature.
That explains what the words do. It does not explain why this particular nonsense word became the first one programmers reach for.
Foo existed before programming examples
In the 1930s, cartoonist Bill Holman scattered the word foo throughout his Smokey Stover comic strip. It appeared on signs, license plates and in deliberately nonsensical slogans. One recurring line played on the fireman’s setting: “Where there’s foo, there’s fire.”
The word escaped the strip. It turned up in cartoons, popular culture and wartime slang. Its precise path is difficult to reconstruct because nonsense travels by jokes, speech and imitation rather than careful attribution. The RFC Editor’s history of foo collects the competing trails and is appropriately dated 1 April 2001. The document is playful, but its bibliography and examples make a serious point: there is no single neat coinage story.
The Second World War acronym FUBAR almost certainly influenced the later sound of foobar. It does not settle the origin of foo. The comic-strip use is earlier, and RFC 3092 notes the possibility that influence ran in more than one direction. “Foo came from FUBAR” is memorable; the evidence is messier.
The MIT bridge
The Tech Model Railroad Club at MIT was one of the places where foo survived as living technical humor. Its members built elaborate control systems beneath their railway layout, and many later became part of MIT’s early computing culture.
The club’s dictionary was first compiled by Peter Samson in 1959. Its online TMRC dictionary preserves foo as a mock-sacred syllable and mentions the duty to keep the “foo counters” turning. This is not yet a clean dictionary definition of a programming placeholder. It is evidence that the word was already part of the club’s shared language before hacker culture carried it further.
That distinction matters. The TMRC did not sit down and decree that every
unknown variable should be named foo. A flexible in-joke moved with people
from workshops into computer labs. Once it appeared in examples and manuals,
new programmers learned the convention even if they never knew the joke.
Foo and bar reach the network
By 1971, the pair was visible in the documents of the young ARPANET. RFC 269,
“Some Experience with File Transfer”,
uses FOO as a sample filename. Its command examples delete, send and retrieve
FOO, then show RN FOO BAR to rename that file to BAR.
Thirty years later, RFC 3092 counted roughly 212 RFCs containing foo, bar
or foobar as placeholders. The words had crossed from local lab slang into
the written infrastructure of computing. Documentation repeated them because
readers already recognized them; readers recognized them because documentation
kept repeating them.
Bar naturally follows foo partly because the pair forms foobar, reinforced
by wartime slang. Baz then gives an example writer a third item without
breaking the rhythm. The Jargon File lists longer continuations, but after
qux the sequence becomes less universal and more like a test of which corner
of hacker culture taught you to program.
When foo is useful — and when it is not
Placeholder names work when the names truly do not matter. A compact example
of function composition may be clearer with foo and bar than with invented
business concepts that make the reader wonder whether invoices and customers
are important to the lesson.
They fail when the names carry meaning. In production code, calculateTax()
usually communicates more than foo(). Even in documentation, a realistic
name can reveal intent that a placeholder hides. The right question is not
whether foo is professional. It is whether the example is teaching a shape
or a domain.
Like counting array positions from zero,
foo and bar are conventions that become almost invisible after you learn
them. To a newcomer they can look arbitrary, because they are. Their usefulness
comes from everyone agreeing on the same arbitrariness.
So foo does not secretly mean a particular function, and bar is not a
special kind of variable. They are old nonsense words that acquired a precise
social role: making everything except the name easier to see.