DEV Community

loading...

No More Foo, Bar, and Baz

warrend profile image Daniel Warren ・2 min read

When I was a kid, my best friend’s dad owned a computer company and told us he knew the secret to Fubar. He said he was sworn to secrecy and to this day has never divulged the secret. If you’re not familiar with Fubar, he was a 1980s robot whose name stood for Futuristic Uranium Bio-Atomic Robot. He seemed so life-like and I dreamed of owning one as a kid. Of course, now that I look back at old Fubar videos, it’s pretty clear Fubar was nothing but a big remote control car with a walkie-talkie strapped to his head, probably operated by a guy in a nearby room.

This brings me to my next point. Maybe they’re not related, but I’d like to talk about the use of “foo, bar, baz” in coding tutorials. I’m not quite sure if foo/bar/baz comes from Fubar, but they sound similar enough to make my point.

For those of you who write tutorials, you do such great work and we are appreciative. Without tutorials, the world of coders would be lost. But, for the love of God, please stop using foo, bar, and baz in your tutorials.

Unfortunately this disease has reached epidemic proportions. I recently ran into a tutorial where the writer explained the concept of associations in ActiveRecord using a similar example to below (I’m code-paraphrasing here):

class Foo < ActiveRecord::Base
  has_many :bazzes
  has_many :bars, through: :bazzes
end 

class Bar < ActiveRecord::Base
  has_many :bazzes
  has_many :foos, through: buzzes
end 

class Baz < ActiveRecord::Base
  belongs_to :foo
  belongs_to :bar
end 
Enter fullscreen mode Exit fullscreen mode

First of all, what the hell is a foo and a baz? We know what a bar is but are we talking about a bar/pub or a parallel bar? And how does it relate to a foo? In what capacity does a foo have many bazzes? For me, so many of these concepts take time to grasp, but make it almost impossible when the examples don’t have concrete meaning grounded in reality. Tutorial writers are much better off using real world examples, like how a tweet belongs to a user, or how a dog has many bones.

So please, keep writing tutorials, but do not use foo, bar, or baz!

Discussion (18)

pic
Editor guide
Collapse
kdgiddyup profile image
Kelly Davis

+100

We learn/teach in code class to name variables with contextual meanings, for a reason: to make readable code. But as you have noted, the real world is infested with examples that use these meaningless abstractions, for no reason but convention. That's a pretty dumb reason to confuse learners.

Collapse
spencerwasden profile image
spenwa • Edited

I agree with your post--thanks! Using foo, bar, and baz can teach non-self-documenting code. As you mentioned with your example the variable should have meaning since the data itself (Ohio State/USC) is a concrete example, e.g.:

let team1 = 'Ohio State'
let team2 = 'USC'
console.log(team1 + ' vs. ' + team2); // ‘Ohio State vs. USC'
Enter fullscreen mode Exit fullscreen mode

(By the way, that example would provide a great way to lead into arrays, where the foo/bar snippet wouldn't as readily)

let team[0] = 'Ohio State'
//etc
Enter fullscreen mode Exit fullscreen mode

I saw this example about overriding in PHP on StackOverflow:

<?php

class Foo {
   function myFoo() {
      return "Foo";
   }
}

class Bar extends Foo {
   function myFoo() {
      return "Bar";
   }
}

$foo = new Foo;
$bar = new Bar;
echo($foo->myFoo()); //"Foo"
echo($bar->myFoo()); //"Bar"
?>
Enter fullscreen mode Exit fullscreen mode

Sure I can figure out what's going on in a few seconds. But all the while I have to not only understand what I'm learning, but ALSO at the same time mentally hold on to the meaning of what foo/bar mean. That is extra mental work, and--especially when you've already been working or learning for many hours--it's extra mental work that just isn't necessary. When I just read the echo comments, it's not apparent what happened. If we write self-documenting code, we can see more quickly and clearly what happened in the code. For example:

<?php

class Base {
   function method() {
      return "base_method";
   }
}

class Extended extends Base {
   function method() {
      return "extended_method";
   }
}

$base = new Base();
$extended = new Extended();
echo($base->method()); //"base_method"
echo($extended->method()); //"extended_method"
Enter fullscreen mode Exit fullscreen mode

Note this is not concreteness--you would probably rarely if ever use these names in real-world code, but concepts are built into the code to teach as you read rather than having to deal with the extra layer of meaningless foo/bar/baz.

Collapse
jminkler profile image
Jarret Minkler

While this is more readable for you, consider someone coming into it as a new programmer. Would they now think that all their parent classes have to be named Base, and child classes called Extended, rather than if it was Foo and nonsensical? Foo should be part of every student's vernacular. Just like x and y for math majors.

Collapse
spencerwasden profile image
spenwa • Edited

If someone is to the point where they're looking into extending classes, it has likely been well established in their mind that a class name is the coder's choice and they likely know that there are general rules (e.g. certain keywords can't be used and that those keywords are case sensitive and can't start with a number, etc., etc.) But to your point, many examples use "My" or something as a prefix for that very reason, and that would make sense to do so in the example above for clarity. I don't think a minor flaw in my example negates the main points of what I was saying. It is your opinion that foo/bar/baz should be part of every student's vernacular, and that may well be true for the sole reason that it became an ill-used tradition at some point thereby warranting that need. If you look an a more extreme example where I was needing to hold on to 15 conceptual pieces you could extend out your foo/bar/baz vernacular to an impressive foo/bar/baz/buz/fuz/foz/faz/friz/biz/boz/bir/bur/fyz/bor/fuu and really write some interesting example code. Before I did the replacements in this example below I could read through it in a few seconds and use the concrete names (users, patients, company, etc) and see exactly what the conceptual goal of the code was. While I understand exactly what is happening on every line of the foo/bar/baz replaced code below, it's not a quick thing to see what the entire section of code is trying to accomplish.

<?php

public class Baz{

}

public class Buz{
    public function getFuz(){
        $fuz0 = new stdClass();
        $fuz0->bir = 'bir0';
        $fuz1 = new stdClass();
        $fuz1->bir = 'bir1';
        return [$fuz0, $fuz1];
    }
}

public class Faz{
    public static function getFaz(){
        $faz = new stdClass();
        $faz->boz = 1;
    }   
}

private function getBar(){
    return new stdClass();
}

public function foo()
{
    $bar = $this->getBar();

    $baz = new Baz();
    $buz = new Buz();

    $buzId = NULL;
    if ($bar->fuz == 'bar' || $bar->fuz == 'boz') {
        $buzId = $bar->id;
    }

    // get list all fuzzes
    $fuz = $buz->getFuz($bar->foz);
    $faz = Faz::getFaz($bar->foz);

    $friz = [];
    foreach ($fuz as $biz) {

        if ($faz->boz == 1 || !$biz->bir) {
            $biz->bir = $biz->id;
        }

        $friz['bur'][$biz->bir]['bur_id'] = $biz->bir;
        $friz['bur'][$biz->bir]['fyz'] = $biz->fyz;
        $friz['bur'][$biz->bir]['bor'] = $biz->bor;
        $friz['bur'][$biz->bir]['fuu'] = $biz->fuu;
    }

    // ...

}
Enter fullscreen mode Exit fullscreen mode

Examples that illustrate a new concept to someone should, in my opinion, not carry the extra conceptual overhead of meaningless names, since understanding the new concept is mental work enough. Commonly people learn by example. Concrete names can be themselves inherent example fragments that contribute to the example as a whole. Without them the overall example is weakened.

Thread Thread
drevil_philippo profile image
InSomnia | Dr.Evil

people are ignorant and they will never change...
examples like the many ones you provides should be more than enough reason to stop using foo/bar

foo/bar users should maybe skip high-level programming all together and write their code in Assembler just addressing registers and shuffling values around -
because it is apparent that they have no interest in producing readable code anyway...

Collapse
drevil_philippo profile image
InSomnia | Dr.Evil

Foo and Bar stuff is INCREDIBLY hard to read/understand.
No matter if you know they are just placeholders or not.
Even var1 and var2 or func1 and func2 would be miles easier to understand than this crap.
My brain tries to assign some sort of logic to a variable while trying to understand the code.
With foo and bar its nearly impossible...

Collapse
marlon_schultz profile image
Marlon Schultz

I agree. It sometimes even held me back, when trying to understand a concept. Mainly because bar and baz sound very similar. That's probably how my brain works. If it would have been called bingo and bongo it would sound as ridiculous as bar and baz, but be more distinguishable for me.

Collapse
warrend profile image
Daniel Warren Author

I agree. It's especially confusing when the words have no meaning and context attached to them. Clarifying examples as something concrete makes tutorials much easier to follow, at least for me.

Collapse
alainvanhout profile image
Alain Van Hout • Edited

They are indeed related, and they serve the same purpose as other generic placeholder words (e.g. thingy, stuff, and John Smith): en.m.wikipedia.org/wiki/Foobar and en.m.wikipedia.org/wiki/Metasyntac...

Collapse
misterkevin_js profile image
Kevin Rodríguez

Nope, John Doe is used in the context of being the name of a person and that's completely ok, but you can't just use Foo, Bar, Baz to refer to a Method, a Class, or a Functionality, that's just too unclear, it's no different than using 'x' or 'o' to name a variable.

Collapse
alainvanhout profile image
Alain Van Hout • Edited

Which is exactly what is done in algebra when 'x' has no meaning except to serve as an example (as is the case for Foo, Bar and Baz).

Collapse
asizikov profile image
Anton Sizikov

I somewhat agree with you, these names are not the best choice, however, they serve a very clear goal: to avoid any connection to the real domain. If someone gives an example with Clients and Orders, you would start thinking that orders should not have a link to the Client, and the OrderRepository would solve the problem in a more elegant way.

Abstracting away the meanings is the same trick which we've learned during the math class. We're not counting apples when we learn basic algebra, but we're working with a, b and c: a(b+c) = ab + ac sort of exercises.

When we see a we understand that it doesn't matter if it represents apples, dollars or vectors, what matter is the way functions are applied. When we see Foo and Bar we should focus on the language feature/pattern, but not the domain itself.

Collapse
warrend profile image
Daniel Warren Author • Edited

Thanks for reading. I agree about using abstract variables (like a(b+c)) to explain basic concepts. And I think something like this is just fine:

let foo = 'Ohio State'

let bar = 'USC'

console.log(foo + ' vs. ' + bar); // ‘Ohio State vs. USC'

But even here, and maybe it’s splitting hairs, wouldn’t it makes sense to just name the variables let osu and let usc? What is the benefit to using foo and bar here over something that expresses what the variable actually means?

I would argue that your algebra example is very different than an association example. When creating a tutorial with associations, or where one piece of code relates to another in some way, it just doesn’t make sense to ever use abstracted names like foo, bar, or baz. I would also argue that if someone reads the tutorial and thinks a join table would be better, they probably already understand the concept of associations pretty well, ha. I think we can have the best of both worlds — focus on the language/pattern but also have the example have real world meaning.

That being said, I’m sure there are cases where it’s just fine to use abstracted names. Thanks for weighing in. I appreciate your input and I think it’s a good discussion to have.

Collapse
stanwmusic profile image
Stan Williams • Edited

I agree absolutely! Still to this day Foo and Bar only serve as an abstract distraction to me, so I would wonder why not use areal world example? I always liked learning code from real working examples,and I always seemed to have a hard time understanding examples using Foo, Bar and Baz, so much so that after a while when I would start reading a tutorial or explanation and saw those they only served to irritate me so much that I was no longer in the mood to learn.

Collapse
onecuteliz profile image
Elizabeth Jenerson

"First of all, what the hell is a foo and a baz?"

Facts.
Made me do an actual LOL. 😛

Collapse
adrriver profile image
AdrianRivera

'Foo' and 'bar' always reminded me of 'FUBAR'; though neither CS, programming, nor anything whatever that's closely related typically has much to say about the military. Okay, so structure is paramount in both - great. What the hell is 'baz'? This is so cutesy as to be repulsive. Imagine any other field using this kind of nonsensical crap so commonly; it's not all that easy to do.

Collapse
codemouse92 profile image
Jason C. McDonald

Good points, thank you! I'm guilty of this...but no more!

Collapse
drbess profile image
Royce

I finally got around to reading this post and thought I was the only one aggravated by the foo, bar and baz examples when reading through tutorials.