Eff You Foo Bar
November 10, 2012
Consider this Ruby code:
if self.new? @new_bar = bar.new @new_bar.baz = new_current_baz_value else self.bars.build(:baz => 7) end
Whoever writes code like this should receive a swift foo in the baz.
In case you didn’t know, variables in programming are used to represent things. Let’s examine:
- foo – not a thing (unless you know your military terminology)
- bar – a place to drink; a rod; 100,000 N/m²
- baz – not a thing
Variable names are important because they illustrate the relationships between objects in code. If you’re using sample code to explain something to someone then variable names are equally, if not more important. Let’s say you have two objects with a hierarchical relationship. Why would you write this:
class Larry def add_moe(name) @tree.add(shemp: self, name: name) end end
when you can write this:
class Node def add_child(name) @tree.add(parent: self, name: name) end end
With proper variable names you don’t need to explain that a Larry is a thing that has one shemp and many moes. It’s obvious to anyone who knows about tree-like data structures. When you use the word ‘shemp’ in your code you have to explain what a ‘shemp’ is, and if you need to explain your variable names then your variable names suck.
Not only are foo, bar, and baz completely uninformative and requiring of external explanation, they are even worse than larry, moe, and shemp because “bar” and “baz” look the same!
Please, stop using foo, bar, and baz. You can do so much better.