Against 'foo' (and 'bar' too)

Nočnica Mellifera - Jun 10 '19 - - Dev Community

About half of code examples include the term ‘foo,’ here’s what it means and why you should never use it in your examples.

Here is what ‘foo’ means:

‘Foo’ doesn’t mean anything, it’s just a commonly used example name for a variable.

Very often in programming we need to name a variable something. A variable is a container for some value, and to be useful that variable should have a name that helps make code readable. In real code I might have a variable like user_heightto let me easily refer to that value inside my code.

Variable names are where foocomes up. Sometimes we need to refer to a variable without knowing even what we should name it. Let’s say we want to describe a function and say, in english: “this takes whatever variable you give it and prints it out twice” our variable might be called user_height, it might be called user_first_name, it might be called user_updated_legacy_variable_setting_LOCKED_v2_updated_index! We just want to say that _whatever _variable you give it, this will print it twice, in many examples, foo is used as an example variable name.

If we were in math class, we’d call this variable ‘x’, and say “this function works like this: print X, then print X”. And we’d immediately understand that it doesn’t matter what X is, we wouldn’t raise our hand and ask “Does this function work if instead of X my variable is named Q?” Yes it works! It doesn’t matter what we name our variables.

Here’s why you should never ever use ‘foo’

Let’s take a step back and assume you didn’t just read a definition of foo. Let me show you why it’s really not a great idea to use in your examples.

Programming is surprisingly general. Most programming languages look kind of similar to each other. Why, look at this snippet of C, a language I do not know:

#include <stdio.h>

int main() {
   const char *foo = "Hello";
   const char *bar = "World!";
   fprintf(stdout, "%s %s\n", foo, bar);

   return 0;
}

That looks pretty readable! I can figure out what most of it is doing. Heck let’s step through it!

#include <stdio.h>

Okay we’re probably bringing in required packages with include, the name of the package is, studio? Oh no, Standard Input/Output > Standard I/O > stdio, got it.

int main() {

Definitely defining a function, probably ‘main’ is a function that gets run by default. Gonna cheat a little here and lean on my knowledge that types are something the C languages care about (at least they care about it more than Javascript), so I bet that int means this function returns an integer, and that ‘return 0’ seems to indicate I’m right.

   const char *foo = "Hello";

Declaring a constant, it’s a ‘char’ type which probably means it’s a ‘character’ type. Doesn’t C have strings? Whatever. I can see it getting set to “Hello” so char must be how strings are stored. Whyfoo though?

   const char *bar = "World!";

Why *bar?

   fprintf(stdout, "%s %s\n", foo, bar);

Fprintf is a weird way to print, but yeah, it’ll go to ‘standard out’ which usually means the console in my experience. And it prints some special characters, looks like space ‘%s’ and a newline ‘\n’, and then foo and bar

   return 0;

Gotta return something, and my guess is it’s gotta be an integer.

}

Okay I defined everything but foo and bar, I’m sure we can work that out, let’s check the C Reference Manual to get a definition of ‘foo’

…….

Huh.

‘Foo’ appears 70+ times in the C Reference manual, but the closest I get to a definition is:

“Lowercase letters and uppercase letters are distinct, such that foo and FOO are two different identifiers.”

And that’s it? What the heck is foo?

Again, both ‘foo’ and ‘bar’ are by definition meaningless nonsense words, used to mean ‘any random label could be on this variable, method, whatever, I chose this to show that nonsense would work.

The use of ‘foo’ in code examples dates back to the 1960’s, and it’s bad. The use of foo as ‘a name that means nothing’ makes your code more difficult to interpret and use for the student.

We have a responsibility to make our work accessible. To make it readable and useful. When we write code to use in production, both the code itself and the accompanying documents must be easy for others to understand, because unreadable code is almost useless.

Does it save time to write only for experienced people? Sure. I write about Heroku all the time and I’m tempted to just say ‘Heroku CLI’ instead of ‘Command Line Interface’ but the use of a whole bunch of acronyms makes my writing much harder to understand.

Foo is a barrier to people understanding your examples, and while all code should be easy to understand your examples should be the easiest part to read!

How to avoid using ‘foo’

Use ‘CLI,’ ‘npm’ and ‘TDD’ enough in your article, and you’ll ensure that you saved 10 minutes writing your article, and made something that no new developers can understand. It takes a few extra letters to type out every initialism (like ‘TDD’), and when the benefit is accessibility it’s worth it. The same is true of ‘foo’ let’s fix this example:

#include <stdio.h>
int main() {
   /* of course, these variable names can be whatever you like */
   const char *first_word = "Hello";
   const char *second_word = "World!";
   fprintf(stdout, "%s %s\n", first_word, second_word);

   return 0;
}

Let’s do another, this time replacing ‘foo’ and ‘bar’ in a random number generator

#include <stdio.h>
#include <stdlib.h>

int main() {
  int count, randomNumber;



  printf("Ten random numbers in [1,100]\n");

  for (count = 1; count <= 10; count++) {
    randomNumber = rand() % 100 + 1;
    printf("%d\n", randomNumber);
  }

  return 0;
}

And that’s it! We’re done. A comment and a rename. And get this: we could redo every example in the C Manual in a few hours.

The result is code that is more readable on every line.

The key points to consider:

  • Does it matter that a variable could be named anything?
  • Could a comment help a new programmer here?
  • Could variable names better describe what’s happening here?

We write to be understood

We all have a responsibility to write information that can be understood. Communication is not a solo project. The next time you set down to write examples to help others, take a moment to consider that someone reading this might well be in their first year of coding. This student is googling to find solutions and when they find ones they can read they’ll feel inspired, energized, and empowered. You can give them that feeling by using meaningful variable names.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .