thoughtbot / guides

A guide for programming in style.

Home Page:https://thoughtbot.com/playbook

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Lonely Operator vs Null Object Pattern vs try! chains vs present? chains

toobulkeh opened this issue · comments

Ruby 2.3.0 introduced the Lonely Operator:

https://bugs.ruby-lang.org/issues/11537

We've talked about this before internally, and I don't think many of us are fans of this, as it encourages the use of nil to represent a meaningful value (when it truly shouldn't represent anything at all). It also makes violating the Law of Demeter really easy. We tend not to use try or try! for the same reason.

So I think it's one of those things that would be used sparingly, if absolutely necessary.

So maybe a line in the guides suggesting a NullObject Pattern instead?

That's what I would do. "Prefer the Null Object pattern over using try, try!, or the lonely operator (&.)." I'm curious what other people think, though.

It feels too early to write up a guideline on how to use this. Plenty of us have existing thoughts on nil and the Null Object pattern, but I'd suggest waiting until we have some Ruby 2.3 projects under our belts before jumping to conclusions.

I'm going to close this. We can revisit the topic with a pull request when we have more usage of this operator.

(Also, I can't think of a guideline for this that fits our guides; our guides are things we agree on, but not a list of decision trees on when to use language features.)