r/ruby • • 1d ago

Call Things What They Are

https://www.saturnci.com/call-things-what-they-are.html
64 Upvotes

25 comments sorted by

18

u/Zestyclose-Turn-3576 1d ago

I definitely approve.

I might go further (recognising that this is not the point of the blog of course) and say that a length is an object with a numerical value and a unit distance, so although it might make sense for a particular system to standardise on either metric or US customary units, in many cases it makes sense for `@wheel.rim.diameter` to return a `Length` object, so you can also `@wheel.rim.diameter.to_f(:inches)` or `@wheel.rim.diameter.to_f(:cm)`.

4

u/jasonswett 1d ago

I love it. (If and only if there's an actual use case for both inches and centimeters, of course.)

3

u/Zestyclose-Turn-3576 1d ago

The domain we work in has the joy of needing a few conversion like this, including stuff like millimetres to inches-rounded-to-the-nearest-sixteenth.

1

u/h0rst_ 12h ago

In case you're confused by the tire sizes below, know that bicycle tire sizing is, by tradition, inconsistent. Road bikes originated in Europe andd use metric sizing. [...] Mountain bikes originated in the United States and give tires sizes in inches

Source: Chapter 6 of the book this article is about

7

u/rebuilt 1d ago

The mars climate orbiter was lost because two types of units were used in the same system.  Ground software sent thruster impulse data in imperial pound-force-seconds, while NASA's navigation software treated the numbers as metric newton-seconds (a 4.45x discrepancy)

6

u/izuriel 1d ago

Also the wrapping of two properties in another class just to discard later and not creating it in the constructor. Maybe the book cleans that up.

6

u/jasonswett 1d ago

Yup, later a Wheel instance gets passed to Gear instead of passing in the individual values, which I think is a good move.

5

u/izuriel 1d ago

Yea that’s the better route.

100% agree on the naming thing. Before AI took over one of my guidelines was “no abbreviations,” a bit strict but it helps coming back to code later. Couple that with naming intent and specifics keeps code readable and easy to understand and modify. I never bought into the “fewer keystrokes” mentality. If typing is so hard then practice it. It is (was) a core feature of the profession.

7

u/jasonswett 1d ago

I always say: thinking is more expensive than typing.

4

u/izuriel 1d ago

Well said.

3

u/nfstern 1d ago

Yeah I couldn't put my finger on it, but there were a lot of things that bothered me about that book and this nailed it.

I bought the book with the expectation of getting good insights on how to do this kind of analysis and design and came away from it. More confused than ever.

3

u/jasonswett 1d ago

It's not you. The emperor wears no clothes.

3

u/nfstern 1d ago

Thank you sir! I thought it was a me problem all along. I was very disappointed with that book.

12

u/pickering_lachute 1d ago

Great read. Even Sandi Metz failing to follow her own advice. Explains why Claude and Codex are so awful at it - their entire training set falls foul of this

13

u/perfectshade 1d ago

Claude/Codex love:

- Reaching for a bigger design than it needs, to impress.
- Reallocating its imaginary budget on variable_name_char_count to prompter-directed-apologies.
- Destroying our trade.

Good article, I prefer expressive variable names over minified ones. We're not so strapped for memory that the legibility of the code should play second fiddle.

6

u/bluerock0427 1d ago

I worked with a guy who was resistant to naming things what they are and he would come up with ridiculous, unrelated names for things. Sometimes he would even use different names for the same concept in different places in the code, because he had this belief that reusing the same name in a different context was bad. He left the company with a huge mess of code to maintain! I'm now using AI to help me untangle the translations of nonsense he wrote throughout the code base.

2

u/Reasonable-Fault2687 18h ago

I’ve experienced that too. The only thing worse are the enablers, who are everywhere, “it’s a style issue”

3

u/jgaskins 7h ago

I agree with some of this, but also `gear_inches` is its own concept. It’s not just the size of the gear. So for that specific instance, she did call it what it was.

2

u/Main-Drag-4975 1d ago

Counterpoint: Her code fits on my phone screen and conveys its point in service of a coherent book-length series of lessons. Yours is what, more explicit?

0

u/joemi 1d ago

The article suggests renaming the ratio method to gear_ratio, while at the same time it suggests renaming gear_inches to size. That's rather inconsistent, to remove the gear_ prefix from one method while adding it to the other. They should either be named gear_size AND gear_ratio (which is redundant since they're already in the Gear class, or better they should be named size AND ratio.

If you're going to get pedantic about naming things, at least get it right.

1

u/perfectshade 23h ago

I think the core insight has been demonstrated sufficiently for the purposes of the article.

somethingswan, yo.

0

u/[deleted] 1d ago

[deleted] — view deleted comment

3

u/GroceryBagHead 1d ago
irb(main):001> 3/2
=> 1
irb(main):002> 3/2.to_f
=> 1.5

0

u/[deleted] 1d ago

[deleted] — view deleted comment

3

u/harsh183 1d ago

Perhaps a better snippet would be

def gear_ratio
  chainring_tooth_count.to_f / cog_tooth_count.to_f
end

That way it's really clear to the reader we're doing float division and not integer division. For better or worse, a lot of languages have this quirk where something needs to be converted to float to indicate we have a float result.

I like Python / (float) and // (integer) a lot for this reason.

2

u/tinyOnion 1d ago

Why not call floating point numbers what they are?

because it's a long standard tradition of type coercion. to_a, to_f, to_s, to_r, etc are all basic parts of basic ruby.

if you really want to do it verbosely use Float(cog_tooth_count) works but to rubists this is not idiomatic.

0

u/[deleted] 1d ago

[deleted] — view deleted comment

2

u/ruby-ModTeam 1d ago

Your post or comment was removed because it didn't meet our standards.

They are:

Demonstrating empathy and kindness toward other people

Being respectful of differing opinions, viewpoints, and experiences

Giving and gracefully accepting constructive feedback

Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience

Focusing on what is best not just for us as individuals, but for the overall community

When in doubt use Non-Violent Communication (NVC) https://en.wikipedia.org/wiki/Nonviolent_Communication