Why do many code examples, especially 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 many code examples, especially tutorials, use the names "Foo" and "Bar" so often? It is almost a standard. For example:
|
|||||||||||||||||
|
|||||||||||||||||
We're looking for long answers that provide some explanation and context. Don't just give a one-line answer: please explain why you're recommending it as a solution. Answers that don't explain anything will be deleted. See Good Subjective, Bad Subjective for more information. |
|||||||||||||||||
|
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:
Update (11/23/2011): @Hugo has a fantastic list of the non-military antecedents. |
|||||||||||||||
|
|
I think it's the phonetic pronouncation of fubar. Which stands for:
|
|||||||||||||||||||||
|
|
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:
|
|||||||||||
|
|
Wikipedia gives this definition of Metasyntactic Variable :
The article also gives common examples of such variables in different programming languages :
|
||||
|
|
|
Here is wikipedia's answer:
http://en.wikipedia.org/wiki/Foo And from RFC 3092:
|
|||||||||||||||
|
|
tl;dr
The use of lone "foo" as a nonsense word is pretty well documented in popular culture in the early 20th century, as is the military FUBAR. (Some background reading: FOLDOC FOLDOC Jargon File Jargon File Wikipedia RFC3092) OK, so let's find some references. STOP PRESS! After posting this answer, I discovered this perfect article about "foo" in the Friday 14th January 1938 edition of The Tech ("MIT's oldest and largest newspaper & the first newspaper published on the web"), Volume LVII. No. 57, Price Three Cents:
The Tech newspaper, a year earlier, the Letter to the Editor, September 1937:
And The Tech in December 1938:
The first documented "foo" in tech circles is probably 1959's Dictionary of the TMRC Language:
These are explained at FOLDOC. The dictionary's compiler Pete Samson said in 2005:
And from 1996's Jargon File 4.0.0:
A year before the TMRC dictionary, 1958's MIT Voo Doo Gazette ("Humor suplement of the MIT Deans' office") (PDF) mentions Foocom, in "The Laws of Murphy and Finagle" by John Banzhaf (an electrical engineering student):
Also 1964's MIT Voo Doo (PDF) references the TMRC usage:
But let's remember this question is about code examples, so let's find "foo", "bar" and "foobar" published in code. So, Jargon File 4.4.7 says of "foobar":
The first published reference I can find is from February 1964, but written in June 1963, The Programming Language LISP: its Operation and Applications by Information International, Inc., with many authors, but including Timothy P. Hart and Michael Levin:
Also includes other metasyntactic variables such as: FOO CROCK GLITCH / POOT TOOR / ON YOU / SNAP CRACKLE POP / X Y Z I expect this is much the same as this next reference of "foo" from MIT's Project MAC in January 1964's AIM-064, or LISP Exercises by Timothy P. Hart and Michael Levin:
It shares many other metasyntactic variables like: CHI / BOSTON NEW YORK / SPINACH BUTTER STEAK / FOO CROCK GLITCH / POOT TOOP / TOOT TOOT / ISTHISATRIVIALEXCERCISE / PLOOP FLOT TOP / SNAP CRACKLE POP / ONE TWO THREE / PLANE SUB THRESHER For both "foo" and "bar" together, the earliest reference I could find is from MIT's Project MAC in June 1966's AIM-098, or PDP-6 LISP by none other than Peter Samson:
Some more recallations. @Walter Mitty recalled on this site in 2008:
John V. Everett recalls in 1996:
Daniel P. B. Smith in 1998:
Robert Schuldenfrei in 1996:
Paul M. Wexelblat in 1992:
Bruce B. Reynolds in 1996:
Here's a straight IBM "BAR" from 1955. Other early references:
I haven't been able to find any references to foo bar as "inverted foo signal" as suggested in RFC3092 and elsewhere. Here are a some of even earlier F00s but I think they're coincidences/false positives: |
|||||
|
|
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:
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:
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. |
|||||||||||
|
|
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.
|
||||
|
|
|
According to http://en.wikipedia.org/wiki/Foo:
|
||||
|
|
|
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. |
|||||
|
|
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() ). |
||||
|
|