26 points · by raganwald · 17y ago · 14 comments
It would be one thing if the pattern saved a lot of unnecessary code, but it doesn't. The assignment of the local variable just becomes the block variable, and you have an 'end' where the method-final return would be. Instead of a simple assignment, you now have the method invocation and the arguably more complicated block syntax.
I'll address the two particular motivations raised by the author specifically because I don't agree with either of them. First, I don't think forgetting to return the variable is a common bug. Anecdotally, I can't ever remember making this mistake myself. Even if I were to, it would be immediately obvious what the problem is. I'm open to the fact that other people perhaps make this mistake, but I'd be surprised if they routinely do. Second, I don't feel like it's obvious at all what the return value of the method is up-front, because the method invocation is confusing if you don't know what it does. Even if you do, it doesn't gain you anything you don't get by simply initializing a local at the start of the method, especially if you call the local return_value or something self-descriptive like that.
This kind of refactoring is the kind that I don't like — refactoring for the sake of refactoring, and making the code less accessible (that is, more complicated and harder to understand) for no discernible benefit.
I encountered bugs like forgetting to return the intialized object from a method as well as assigning to the parameter of a return invocation a number of times when working with a large Rails code base. But if you have never encountered it, you need never use #returning in any of its forms.
So really, I'm not arguing with you. Different people, different experiences, different approaches to code. I like #returning, I like #let, I like a lot of things that impose block structure on code. Others do things differently with my blessing.
But I'm often (usually?) wrong — I was just putting in my two cents for the sake of discussion =)
No, scratch that, instead I'll say this: I don't buy the argument that #returning, or SymbolToProc, or any of the other myriad (and often irritating) little idiomatic extensions to Ruby make code less complicated and more difficult to read.
It's an old, old argument and tiresome to rehash, but ultimately it's a mistake to make things easier to read for someone who doesn't know the idioms at the expense of making things easier to read for somone who does know the idioms.
If you feel the idiom doesn't add value, that's one thing. But I have trouble with the idea that #returning is harder to read. I personally find it easier to read because it is so obvious what is going on.
http://blog.rubybestpractices.com/posts/gregory/011-tap-that...
foo().bar().blitz()
becomes: foo().bar().tap { |b| puts b.inspect }.blitz()
It takes its inspiration from Unix's "tee."The more standard way goes from top to bottom. The lambda method requires the reader to go to the bottom of the statement, then to the top and downward. It requires the writer to remember the idiom and it's a barrier to newer developers.
I also don't see the win in doing this either. Sure, somebody can forget to set the return value, but you can also screw up with the other idiom. That part seems like a wash to me.
* edit - Grammatical corrections and reformatting.
returning [1] do |numbers|
numbers << 2
numbers += [3]
end
And that's all you see in your source code. RewriteRails rewrites that into: lambda do |numbers|
numbers << 2
numbers += [3]
numbers
end.call([1])
And that's what the ruby interpreter sees. But you need never look at the "ugly" rewritten version, just what you wrote.