Step 2: Choose Good Names
Looking at the class code again, one thing that makes this code harder to read is that the variable names are short—like m for meter or r for report.
| def print(m) # m = meter |
| r << "Type: Gas\n" # r = report |
There’s nothing wrong with short variable names themselves. But when they hide or make the intent of the code harder to read, that’s usually a sign that we want to change them.
Let’s go ahead and expand those names and use whole words to better describe what they represent. Tweaking the names a bit, here’s what we have now:
| class MeterPrinter |
| |
| def print(meter) |
| |
| report = StringIO.new |
| |
| case meter.type |
| when 'gas' |
| report << "Meter Report\n" |
| report << "Type: Gas\n" |
Get The Way of the Web Tester now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.