Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 10, 2019 06:27 pm GMT

Against 'foo' (and 'bar' too)

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

Here is what foo means:

Foo doesnt mean anything, its 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. Lets 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, wed call this variable x, and say this function works like this: print X, then print X. And wed immediately understand that it doesnt matter what X is, we wouldnt raise our hand and ask Does this function work if instead of X my variable is named Q? Yes it works! It doesnt matter what we name our variables.

Heres why you should never ever use foo

Lets take a step back and assume you didnt just read a definition of foo. Let me show you why its 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 lets step through it!

#include <stdio.h>

Okay were 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 Im right.

   const char *foo = "Hello";

Declaring a constant, its a char type which probably means its a character type. Doesnt 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, itll 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 its gotta be an integer.


Okay I defined everything but foo and bar, Im sure we can work that out, lets check the C Reference Manual to get a definition of foo



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 thats 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 1960s, and its 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 Im 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 youll 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 its worth it. The same is true of foo lets 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;}

Lets 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 thats it! Were 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 whats 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 theyll feel inspired, energized, and empowered. You can give them that feeling by using meaningful variable names.

Original Link:

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To