On page 38 of Practical Object-Oriented Design in Ruby (POODR) is the following code snippet, one that recurs in some form many times throughout the book.
class Gear
attr_reader :chainring, :cog, :rim, :tire
def initialize(chainring, cog, rim, tire)
@chainring = chainring
@cog = cog
@rim = rim
@tire = tire
end
def gear_inches
ratio * Wheel.new(rim, tire).diameter
end
def ratio
chainring / cog.to_f
end
# ...
end
class Wheel
attr_reader :rim, :tire
def initialize(rim, tire)
@rim = rim
@tire = tire
end
def diameter
rim + (tire * 2)
end
# ...
end
puts Gear.new(52, 11, 26, 1.5).gear_inches
This code has always offended me. It violates one of the most simple and fundamental rules in software design: Call Things What They Are.
Sadly, many programmers, judging by the code I read, hate the Call
Things What They Are rule. If something is a page_count, for
example, they'll call it a page, a count,
anything but what it actually is. In fact, a resistance to calling things
what they are isn't just a trait of the worst programmers, it seems to
actually be the norm. What's extra sad is that LLMs have picked up this
bad habit. If I had a dollar for every time I had to tell Codex "call
things what they are for fuck's sake!!!" (even though I already told it
in AGENTS.md) I would be a very rich man indeed.
Unfortunately, Sandi Metz, the author of POODR, seems to share this
common resistance to calling things what they are. Let's take a closer
look at the #diameter method from the example above.
class Wheel
def diameter
rim + (tire * 2)
end
end
I would have thought that the thing called tire represents a
tire. But it must not be, since you can't multiply a tire by 2. tire
must be some sort of numerical value. Of what? Apparently we multiply
tire by 2 and add it to rim. What's rim then?
Maybe that's a clue.
By now I obviously know better
than to think rim means rim. Like tire, rim
must also be a numerical value. I'll be honest with you, dear reader: I
couldn't figure out the meanings of these variables by thinking through
it on my own, nor by flipping through the book's pages. I had to go and ask Claude.
It told me that tire represents the thickness of the tire and
rim means the diameter of the rim. Obvious in hindsight, I suppose,
but I was sincerely stumped.
If it were me, the names I would have given to "rim diameter" and "tire
thickness" are rim_diameter and tire_thickness.
Let's see how the code reads with those two names replaced.
class Wheel
def diameter
rim_diameter + (tire_thickness * 2)
end
end
See how the meaning just pops out when you Call Things What They Are?
Just for fun, let's label all the cases in the Gear class
where something is called something other than what it actually is.
There are a lot of them.
class Gear
attr_reader :chainring, :cog, :rim, :tire
def initialize(chainring, cog, rim, tire)
@chainring = chainring # chainring_tooth_count
@cog = cog # cog_tooth_count
@rim = rim # rim_diameter
@tire = tire # tire_thickness
end
def gear_inches # size
ratio * Wheel.new(rim, tire).diameter
end
def ratio # gear_ratio
chainring / cog.to_f
end
end
puts Gear.new(52, 11, 26, 1.5).gear_inches
The final reckoning: 0 for 4 on the variables and 0 for 2 on the method
names. Ouch. Let's address the method names first,
gear_inches and ratio.
Since gear_inches is an instance method on the Gear
object, the code that uses gear_inches might look something like this:
gear = Gear.new(52, 11, 26, 1.5)
puts gear.gear_inches
It doesn't make much sense. A gear doesn't have a gear. A gear has a size. The bicycle has a gear. Look how much clearer this is:
gear = Gear.new(52, 11, 26, 1.5)
puts gear.size_inches
Another problem is the inconsistent application of the _inches
suffix. Are we suffixing all our inch values with _inches or
are we not? It certainly doesn't make sense to to just arbitrarily use that
suffix for some inch values but not others. Here's the full POODR snippet,
but following the Call Things What They Are rule and with the unit suffix
inconsistency fixed.
class Gear
attr_reader :chainring_tooth_count, :cog_tooth_count, :rim_diameter_inches, :tire_thickness_inches
def initialize(chainring_tooth_count, cog_tooth_count, rim_diameter_inches, tire_thickness_inches)
@chainring_tooth_count = chainring_tooth_count
@cog_tooth_count = cog_tooth_count
@rim_diameter_inches = rim_diameter_inches
@tire_thickness_inches = tire_thickness_inches
end
def size_inches
gear_ratio * Wheel.new(rim_diameter_inches, tire_thickness_inches).diameter_inches
end
def gear_ratio
chainring_tooth_count / cog_tooth_count.to_f
end
# ...
end
class Wheel
attr_reader :rim_diameter_inches, :tire_thickness_inches
def initialize(rim_diameter_inches, tire_thickness_inches)
@rim_diameter_inches = rim_diameter_inches
@tire_thickness_inches = tire_thickness_inches
end
def diameter_inches
rim_diameter_inches + (tire_thickness_inches * 2)
end
# ...
end
puts Gear.new(52, 11, 26, 1.5).size_inches
Much easier to understand. I can't tell you how many times I've done a sweep through a piece of legacy code, changing nothing but the variable names, and seen an utter transformation in the understandability of the whole.
Many concepts in software design are hard to learn: test-driven development, object-oriented programming, abstraction, domain modeling. Learning to simply Call Things What They Are is trivial. The payoff is immense. All you have to do is invest a few minutes of thought on your variable names up front, then you reap the rewards over and over forever.