DEV Community

Discussion on: No More Foo, Bar, and Baz

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
 
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
 
codypellom profile image
CodyPellom

No.

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...

Thread Thread
 
warrend profile image
Daniel Warren

Been years since I wrote this, but I don’t think there is ever an appropriate time to use foo/bar variables.