Eff You Foo Bar
November 10, 2012 — Code
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.
More CodeNov 06, 2008 Form Builders in Rails
Oct 06, 2009 Simulate Has Many Through HABTM
Dec 09, 2009 Single Table Inheritance in Rails
Jan 18, 2010 Anatomy of a Ruby Web Application
Mar 09, 2010 Sending Email From Your Web Site
Apr 13, 2011 What the ɊȱɁͲ Is UTF-8? A Character Encoding Primer
Jun 29, 2009 Punting on Feedzirra Dependencies
Sep 15, 2009 Build Web Applications In Static HTML