Eric Hodel wrote:
Had use for this today: #returning is a convenience method you'll find
in both Facets and ActiveSupport. Jamis' blogged it the other day:
http://weblog.jamisbuck.org/2006/10/27/mining-activesupport-object-
returning
Any one else think this is worthy of standard Ruby?
From the blog post:
The latter is no shorter than the former, but it reads more
elegantly. It feels more "Rubyish". And there are times that it
can save you a few lines of code, if that's important. (Just scan
the Rails source code for more examples.)
So... it usually doesn't save any lines, it adds typing, it hides the
object you're returning in the middle of a statement and it is
significantly more expensive to execute[1]. (Oh, but maybe, in some
cases, it might save you one or two lines of code.)
Er... that seems a backwards and inconsitant interpretation. How can it
add typing if can save a line or two of code?
Most of the time it adds code. Look at the two examples on the blog post. In the returning case I add "returning do || end" in the simple case I have an extra "=".
The example shows in the blog post shows returning saving exactly 0 lines of code. It claims that in some cases it saves lines, but doesn't give any examples, so it is probably a very rare statement.
Moreover it is optional usage, so use it when it saves you the lines and not otherwise, or if it simply adds to the readility, and not otherwise.
If it commonly saved lines then I'd expect the blog post would have used such an example. I doubt that it commonly saves lines (the blog post implies it usually does nothing to the line count).
As for "hides the object you're returning in the middle of a statement", how's that? The object is stated at the very beginning.
In most every other place in ruby the last line of a code grouping construct is the return value. This is the way I've been reading my Ruby for years. With #returning the return value is buried in the middle of the line between "returning" and "do".
It's very readible. It says: here is the object we will return after doing the following things to it.
That's extra state I have to keep in my brain to keep the code straight.
Traditional code doesn't require me to hold extra state in my head because its linear. Linear is simpler and simpler is more readable.
Without it you have no idea what the goal is --return statements could be deeping embedded inside the rest of the code.
This statement is orthogonal to the use of #returning. You may throw in extra returns anywhere you like regardless of the #returning block.
Now, I grant you the speed issue sucks --and if that's important, again
you have the option of not using it. But also, if it were built in
perhaps it could be optimized (?)
Its simpler to just not have it at all. #returning gives no great benefit and has many downsides.
No thanks.
I don't think that makes it more rubyish. Every time I've
encountered #returning in Rails I've found it decreased the
readability of the code and uselessly increased the complexity.
(Wait, now what does this return? oh, yeah, way up there.)
Well, a def can easily get "mucky". #returning can help provide some
structural constraint. Most the time it isn't needed, but on occasion I
find it helps.
#returning isn't going to add any structure since it doesn't really do anything. You're really just replacing 'var' with 'end' and adding a bunch of whitespace.
Besides that the 'tap' functionality can be very hepful in it's own
right.
So what? I didn't mention #tap in my email.
···
On Nov 15, 2006, at 2:50 PM, Trans wrote:
On Nov 8, 2006, at 6:40 AM, Trans wrote:
--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant
http://trackmap.robotcoop.com