Foo/bar decreases readability

I think using foo or bar in code snippets is detrimental when visually parsing the code–especially when the syntax is unfamiliar. For example, I was browsing through some php code samples to find this:

foreach ($bar as $foo) { 
  // do some stuff with $foo 
}

Great. Now, say I don’t know php; is foo an element or the array? From the comment, it’s the array. Then I alt-tab, scroll through some code, and immediately lose 80% of what I gleaned from the context switch, only recalling “bar as foo”. That means absolutely nothing. But if the snippet avoided the non-informative foobar usage, it would be so much more intuitive.

foreach ($files as $file) { 
  // do some stuff with $file
}

Files as file… I’ll remember that easier than “foo as bar”. Using some simple, practical variable names makes example code more clear. Applying this to a function:

void foo(int bar1, int bar2);

versus

void func(int arg1, int arg2);

Another foo-embedded code snippet:

xargs -ifoo sh -c 'tar zxvf foo.tar.gz && mv $(echo foo | sed s/-.*$//) foo'

Now why not have “file” instead of “foo”? It probably would make more sense.

Leave a Reply