Kurt Stephens

Nerd Up!

Recent comments

Random image

Latest image

Syndicate

Syndicate content

Browse archives

« November 2008  
Mo Tu We Th Fr Sa Su
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15
17 18 19 20 21 22
24 25 26 27 28 29 30

Love it


This definitely results in more succinct code. The only downside other than breaking code that depend on nil’s throwing exceptions would be isolating the points in the method chains that result in the origiNil values.

I definitely agree that code like this is a bad idiom that I have seen all over the place:

def foo; bar && bar.baz && bar.baz.caz("x"); end

And code like this is equally bad in that it over-eagerly traps errors you might want to know about otherwise:

def foo; bar.baz.caz('x') rescue nil; end

I had considered implementing a proxy for a while, but it felt at least as visually expensive and probably computationally too:

def foo; bar.if.baz.caz('x').else(nil); end

There’s a nice sense of simplicity in your solution. It clashes with the WhinyNil approach of ActiveSupport/Rails – though I see that with good test coverage the impact on losing warnings caused by method calls on nil might be negligible.

Cool idea.

Reply

The content of this field is kept private and will not be shown publicly.
Captcha Image: you will need to recognize the text in it.
Please type in the letters/numbers that are shown in the image above.