The Origins of Common Variable Names

When diving into programming, especially when learning from tutorials or examples, you often encounter variable names like foo, bar, i, or baz. These seemingly cryptic names appear across numerous languages and contexts. Ever wondered why they’re so pervasive? In this post, we’ll explore the history and reasoning behind these common placeholder names.

foo and bar: The Ubiquitous Pair

Origin

The variable names foo and bar have their roots in Unix, and they are part of a longer tradition in computer science. The term “foobar” has been used as a placeholder in programming and computer science education for decades. The etymology of “foobar” traces back to the military slang term “FUBAR,” an acronym meaning “Fouled Up Beyond All Recognition” (or a more explicit version of “fouled”). This slang was popularized during World War II and later found its way into the computing world.

Usage

In programming, foo and bar are used as generic placeholders, often when the actual variable name is unimportant, or when the focus is on the structure or syntax rather than the specifics of the code. They’re frequently used in:

  • Code Examples: Demonstrating syntax or concepts without cluttering the example with domain-specific names.
  • Testing: Placeholder variables for quick tests or prototyping.
  • Educational Contexts: Illustrating programming principles without the distraction of meaningful names.

Here’s an example of how foo and bar might appear in a piece of code:

function add(foo, bar) {
    return foo + bar;
}

console.log(add(3, 4)); // Outputs: 7

Variations

Beyond foo and bar, you’ll often encounter baz, qux, quux, and corge. These follow a pattern of nonsensical naming, continuing the tradition of placeholder names for multiple variables:

def calculate(foo, bar, baz):
    return foo \* bar + baz

result = calculate(2, 3, 5)
print(result) # Outputs: 11

i: The Indispensable Index

Origin

The variable i is predominantly used as a loop counter or index variable. Its origin can be traced back to mathematics, where “i” is often used to denote an index in summations and sequences. This practice carried over into programming, where i became synonymous with iteration.

Usage

In programming, i is almost universally recognized as the primary index variable for loops, particularly in languages that support C-style for-loops:

for (int i = 0; i < 10; i++) {
    printf("%d\n", i);
}

In this example, i serves as a counter that increments with each iteration of the loop.

Variations

Other variables such as j, k, and n are often used when nested loops are involved, following a sequential order for clarity and readability:

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        System.out.println("i: " + i + ", j: " + j);
    }
}

In summary, while these variable names might seem trivial or whimsical at first glance, they play a vital role in the world of programming, acting as the glue that holds countless lines of code together.