Why do code examples (mostly tutorials) use the names "Foo" and "Bar" so often? It is almost a standard.
For example:
void foo(char* bar) {
printf("%s", bar);
}
Why do code examples (mostly tutorials) use the names "Foo" and "Bar" so often? It is almost a standard.
For example:
void foo(char* bar) {
printf("%s", bar);
}
From http://en.wikipedia.org/wiki/Foobar
In technology, the word was probably originally propagated through system manuals by Digital Equipment Corporation in 1960s and early 1970s. Another possibility is that foobar evolved from electronics, as an inverted foo signal. This is because if a digital signal is active low (so a negative or zero-voltage condition represents a "1") then a horizontal bar is commonly placed over the signal label. The Jargon File makes a case that foo possibly predates FUBAR
From the Jargon Files http://www.catb.org/jargon/html/F/foo.html
foo: /foo/
bar: /bar/, n.
Let me add that it's a cultural thing. In Italy we use other common names for generic variables, files etc.
We commonly use the (italian) names of Disney characters.
The New Hacker's Dictionary has a very good entry on this - and I consider it to be a better resource for this kind of thing than Wikipedia:
metasyntactic variable /n./
A name used in examples and understood to stand for whatever thing is under discussion, or any random member of a class of things under discussion. The word foo is the canonical example. To avoid confusion, hackers never (well, hardly ever) use `foo' or other words like it as permanent names for anything. In filenames, a common convention is that any filename beginning with a metasyntactic-variable name is a scratch file that may be deleted at any time.
To some extent, the list of one's preferred metasyntactic variables is a cultural signature. They occur both in series (used for related groups of variables or objects) and as singletons. Here are a few common signatures:
foo, bar, baz, quux, quuux, quuuux...:
MIT/Stanford usage, now found everywhere (thanks largely to early versions of this lexicon!). At MIT (but not at Stanford), baz dropped out of use for a while in the 1970s and '80s. A common recent mutation of this sequence inserts qux before quux.bazola, ztesch:
Stanford (from mid-'70s on).foo, bar, thud, grunt:
This series was popular at CMU. Other CMU-associated variables include gorp.foo, bar, fum:
This series is reported to be common at XEROX PARC.fred, barney:
See the entry for fred. These tend to be Britishisms.corge, grault, flarp:
Popular at Rutgers University and among GOSMACS hackers.zxc, spqr, wombat:
Cambridge University (England).shme
Berkeley, GeoWorks, Ingres. Pronounced /shme/ with a short /e/.snork
Brown University, early 1970s.foo, bar, zot
Helsinki University of Technology, Finland.blarg, wibble
New Zealand.toto, titi, tata, tutu
France.pippo, pluto, paperino
Italy. Pippo /pee'po/ and Paperino /pa-per-ee'-no/ are the Italian names for Goofy and Donald Duck.aap, noot, mies
The Netherlands. These are the first words a child used to learn to spell on a Dutch spelling board.Of all these, only
foo' and
bar' are universal (and baz nearly so). The compounds foobar and `foobaz' also enjoy very wide currency.Some jargon terms are also used as metasyntactic names; barf and mumble, for example. See also Commonwealth Hackish for discussion of numerous metasyntactic variables found in Great Britain and the Commonwealth.
I always thought foo bar came from the pronunciation of FUBAR. (F*cked up beyond all reason)
It has the nice side effect of making it obvious to the experienced reader that the given code sample is just an example.
You could see it as a warning agains uncontrolled copy&paste...
Within the Python community, the right counterparts of foo and bar are spam and eggs.
Something on the famous phrase : The Jargon File - Foobar. I think it sounds
there's an RFC on the matter: http://www.faqs.org/rfcs/rfc3092.html
I think its the phonetic pronouncation of fubar.
Which stands for:
People also use baz and quux, which I can't understand at all.
I prefer, if I'm needing more than two trash vars, to use:
snafu, tarfu, fubar, soylent
I second the jargon file regarding Foo Bar.
I can trace it back at least to 1963, and PDP-1 serial number 2, which was on the second floor of Building 26 at MIT. Foo and Foo Bar were used there, and after 1964 at the PDP-6 room at project MAC.
using words like "foo" and "bar" make you focus on the concept not on what you can grasp based on the terms you know. For example:
public abstract class Animal
{
public abstract void speak();
}
public class Cat
extends Animal
{
public abstract void speak()
{
System.out.println("meow");
}
}
public class Dog
extends Animal
{
public abstract void speak()
{
System.out.println("bark");
}
}
The above code lets you fall back on your knowledge of real world things.
If you are trying to explain a concept where the important part is not what is being done (printing meow or bark for example) but on how it is being done then removing the parts that you are familiar help:
public abstract class Foo
{
public abstract void star();
}
public class Bar
extends Foo
{
public abstract void star()
{
System.out.println("A");
}
}
public class Car
extends Foo
{
public abstract void star()
{
System.out.println("b");
}
}
Now you have to focus on what is really happening, you are no longer able to guess at what is going to happen.
So, the short version is, that foo, bar, and the like, are used to stress concepts where the content doesn't really matter but the idea does.
Here is wikipedia's answer:
The terms foobar, foo, bar, and baz, are common placeholder names (also referred to as metasyntactic variables) used in computer programming or computer-related documentation. They are commonly used to represent unknown values, typically when describing a scenario where the purpose of the unknown values are understood, but their precise values are arbitrary and unimportant. The terms can be used to represent any part of a complicated system or idea, including the data, variables, functions, and commands. The words themselves have no meaning in this usage, and are merely logical representations, much like the letters x and y are used in algebra. Foobar is often used alone; foo, bar, and baz are usually used in that order, when multiple entities are needed.
Foo has entered the English language as a neologism and is considered by many to be the canonical example of a metasyntactic variable.[citation needed] It is used extensively in computer programming examples (sometimes expressed as "for once only") and pseudocode. Eric S. Raymond has called it an "important hackerism" alongside kludge and cruft.[1]
http://en.wikipedia.org/wiki/Foo
And from RFC 3092:
When used in connection with 'bar' it is generally traced to the WW II era Army slang acronym FUBAR ('Fucked Up Beyond All Repair'), later modified to foobar. Early versions of the Jargon File [JARGON] interpreted this change as a post-war bowdlerization, but it now seems more likely that FUBAR was itself a derivative of 'foo' perhaps influenced by German 'furchtbar' (terrible) - 'foobar' may actually have been the original form.
For, it seems, the word 'foo' itself had an immediate prewar history in comic strips and cartoons. In the 1938 Warner Brothers cartoon directed by Robert Clampett, "The Daffy Doc", a very early version of Daffy Duck holds up a sign saying "SILENCE IS FOO!"...(snip)
It's meaningless, like "widget". It allows you to use a word without worrying about what word to use. See here for more.
For a non-programming use, see Foo Fighter (not the band).
According to http://en.wikipedia.org/wiki/Foo:
"The terms' origins are not known with certainty, and several anecdotal theories have been advanced to identify them. Foobar may derive from the vulgar military acronym FUBAR, or it may have gained popularity due to the fact that it is pronounced the same."
As far as I know, foo comes from foobar which is an alteration of "fubar", a military catch phraase that stands for "F***ed up beyond all recognition."
Then again, there may be other sources.
Foo and Bar (otherwise known as FUBAR...F***ed Up Beyond All Recognition) has just been used as standard generic names for things like classes, properties, method names, etc.
Basically the idea is to convey your code without as much extraneous information that could possibly get away from how the code works (i.e. we don't need to know your function is named AddUser to see the code and understand what it does if we rename it to Foo() ).
Perhaps Jeff's answer, further linking the jargon file, would be better (as I source checked myself before posting a notional linkage to FUBAR):
Foo and bar come from the US Army WWII acronym FUBAR, "F-ed Up Beyond All Recognition". A whole family of these terms came into widespread use during the North African and Sicilian campaigns (1942-43). Rick Atkinson's excellent Day of Battle: The War in Sicily and Italy, 1943-1944 gives a list of these. For instance a JANFU is a "Joint Army Navy F Up", such as the incident on 11 July 1943 when the invasion fleet for Operation Husky shot down 23 Army Air Force C-47 transports carrying paratroopers to reinforce the beachhead.
Update: Wikipedia has a list of related acronyms that includes some the original WWII ones listed by Atkinson.
Any programmer will understand the motivation for using foo and bar to name variables. They certainly have been part of the C/UNIX culture from the start, and as @Walter Mitty points out, predated it.
Update (10/5/2009): Here's Atkinson's description:
Their pervasive "civilianness" made them wary of martial zeal. "We were not romantics filled with cape-and-sword twaddle," wrote John Mason Brown, a Navy Reserve lieutenant headed to Sicily. "The last war was too near for that." Military life inflamed their ironic sensibilities and their skepticism. A single crude acronym that captured the soldier's lowered expectations -- SNAFU, "situation normal, all fucked up" -- had expanded into a vocabulary of GI cynicism: SUSFU (situation unchanged, still fucked up); FUMTU (fucked up more than usual); JANFU (joint Army-Navy fuck-up); JAAFU (joint Anglo-American fuck-up); FUAFUP (fucked up and fucked up proper); and FUBAR (fucked up beyond all recognition) [Atkinson, p. 36].
to make things simple and let you focus on the concepts, of the examples
Here in Japan many people use "hoge", "piyo" and "fuga" instead of foo bar baz.