From: Joel VanderWerf Date: 2006-11-17T06:57:52+09:00 Subject: Re: #returning and #tap Eric Hodel wrote: > On Nov 15, 2006, at 5:40 PM, Joel VanderWerf wrote: >> Eric Hodel wrote: >> ... >>> 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.) >>> 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.) >> >> This variant saves typing and IMO is more idiomatically ruby: >> >> class Object >> def then >> yield(self) >> self >> end >> end >> >> def foo >> @foo ||= Foo.new.then do |f| >> f.bar = "bar" >> end >> end > > $ wc > [...] > 5 12 67 > >> Otherwise you have to do this: > > I would write it: > > def foo > return @foo if @foo > @foo = Foo.new > @foo.bar = 'bar' > @foo > end > > $ wc > ... > 6 14 77 > > Ten characters and one line more typing, but less punctuation. (And > those ten extra characters will be mostly be handled by my tab key.) But you have to type @foo _five_ times, instead of once. Oh, well. It's a matter of taste. -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407