Robert Klemme
11/13/2007 10:25:00 AM
2007/11/13, furtive.clown@gmail.com <furtive.clown@gmail.com>:
> On Nov 13, 3:12 am, Robert Klemme <shortcut...@googlemail.com> wrote:
> >
> > IMHO introducing temporary variables with proper names can go a long
> > way to make this piece much more readable. (The same holds true for
> > method names.)
>
> OK, point taken about the proper name. Nonetheless I am surprised
> this is not a unanimous slam dunk. The contenders are:
>
> all_inputs = source_files +
> transform1( stems.map { |f| f + "." + guess_extension(f) } )
> all_files = transform2( all_inputs ).map {|f| File.join dir, f}
>
> verses
>
> all_files = stems.map { |f|
> f + "." + guess_extension(f)
> }.as { |all_inputs|
> transform2(source_files + transform1(all_inputs))
> }.map { |f|
> File.join dir, f
> }
>
> Since I have been accustomed to using #as for years, the latter looks
> canonical and the former looks quirky and too clever. Three things
> come to mind:
>
> (1) Parentheses adjacent to brackets are an eyesore.
I don't subscribe to that. It depends on the individual case.
> (2) The latter is step-by-step linear and easier to follow: one
> transformation, then another, then another. Simple. The former takes
> much more studying in order to understand, relatively speaking. That
> is, my strategy for understanding it is to work from the inmost
> expression outward. But in the latter case, I work linearly from
> start to finish.
As I said, preferences differ. Since you are used to working this way
it's easy for you. For me it took quite a bit of time until I
understood the logic.
> (3) What is the purpose of all_inputs? Is it there as an
> intermediate, or do you want to use it for something else? Should I
> keep a mental note of it, or should I discard it in my mind? In the
> former case, I am left wondering all of this. In the latter case the
> answer is clear: it's a temporary for building all_files, to be
> forgotten as soon as possible.
It never occurred to me that this could be a problem. If you have too
many temporary variables in a method the method is probably too long
or complex anyway.
> I am a lone rubyist who went off the grid several years ago after
> contributing a project on rubyforge, so perhaps *my* style is the
> quirky one. Still, in my mind the latter is sheer elegance compared
> to the former. I actually find it interesting that someone could
> possibly disagree. Do (1)-(3) make any sense, then?
Partly but I'd rather hear other voices as well.
Btw, what is all this transforming doing? Can you give a short
description of what this piece of code is supposed to do? As far as I
can see you have inputs source_files and stems and get a single list
of filenames out of this. Can you give some more hints about the
semantics?
Kind regards
robert
--
use.inject do |as, often| as.you_can - without end