I'm reading this. What's the benefit of using this:
user&.address&.state
over
user.try(:address).try(:state)
I still don't get it.
I'm reading this. What's the benefit of using this:
user&.address&.state
over
user.try(:address).try(:state)
I still don't get it.
(6) Speed
Safe navigation is almost 4 times faster than using the try method from activesupport
Output
(1)
&.
is generally shorter thantry(...)
Depending on the scenario, this can make your code more readable.
(2)
&.
is standard Ruby, as opposed totry
The method
try
is not defined in a Ruby core library but rather in a Rails library. When you are not developing a RoR web app but instead are writing e.g. little helper scripts, this will get relevant pretty fast. (I prefer Ruby over Bash, for example.)(3)
&.
makes debugging easierThe safe traversal operator will throw an error if a nonexistent method is being invoked.
Only on
nil
it will behave leniently:The
try
method will always returnnil
.Note that this is the case with most recent versions of Ruby and Rails.
Now imagine a scenario where a method named
glubsch
exists. Then you decide to rename that method but forget to rename it in one place. (Sadly, that can happen with ruby...) With the safe traversal operator, you will notice the mistake almost immediately (as soon as that line of code is executed for the first time). Thetry
method however will happily provide you with anil
and you will get anil
related error somewhere downstream in program execution. Figuring out where such anil
came from can be hard at times.Failing fast and hard with
&.
makes debugging easier than blithely returningnil
withtry
.EDIT: There is also the variant
try!
(with a bang) that behaves the same as&.
in this regard. Use that if you don't like&.
.(4) What if I don't care if a method is implemented or not?
That would be strange. Since that would be an unexpected way of programming, please make it explicit. For example by fleshing out the two cases (implemented or not) using
respond_to?
and branch off of that.(5) What about
try
's block form?Instead of a method name, a block can be passed to
try
. The block will be executed in the context of the receiver; and within the block there is no leniency applied. So with just a single method call, it will acutally behave the same as&.
.For more complex computations, you might prefer this block form over introducing lots of local variables. E.g. a chain of
would allow
foo
to benil
but requirefoo.glubsch
to return a non-nil
value. Then again, you can do the same with the safe traversal operator in a more concise fashion:Using
try
's block form for complex computations IMHO is a code smell, though, because it impedes readability. When you need to implement complex logic, introduce local variables with descriptive names and maybe use anif
to branch off anil
case. Your code will be more maintainable.HTH!
I don't think we should be comparing those two things, because they do something else.
Given this example
Person
classtry
try
should be used if you're not sure whether the given method exists on the object or not.&.
Safe nagivation should be used if you're not sure if the object you're calling your method on is nil or not
You can't use them interchangeably
People often confuse those two methods, because you can use
try
instead of&.
But you cannot use
&.
instead oftry