in reply to Re^2: "advanced" Perl functions and maintainability
in thread "advanced" Perl functions and maintainability
Nothing, but the way you state this suggests there's something wrong with map and/or grep. Why is that?
I never said that there was something wrong with grep or map, I said that the original poster's usage of grep and map combined--which didn't even work--was bad, and that in general I preferred foreach over greps and maps with multi-statement blocks.
Neither is inherently more clear than the other - people finding one construct more clear than the other usually base this preference (knowingly or not) on their experience with other languages.
I base my preference on the knowledge that variable names are perhaps the single most powerful form of documentation programmers have available to them, and one construct encourages you to give a name to list iterator while the other doesn't. Furthermore, map blocks are indeed meant to be short because each one is evaluated for its result. Thus, if you have a single statement map block like this:
my @content = map {$_->content} @responses;
It all works out nicely, but if you want to do anything more complicated, you end up combining multiple statements into one, like so:
my @content = map { escape_html(convert_newlines($_->content)) } @resp +onses;
Or as Brian showed, separating them and then tacking on the result at the very end to make sure the block gets evaluated properly:
my @content = map { my $content = $_->content; convert_newlines($content); escape_html($content); $content; } @responses;
And not only that, in my personal aesthetic opinion, grep/map combined with $_ are just plain uglier than foreach is, especially when you need to toss in a "+" or two to help the parser, as the OP did.
What is objectionable is to suggest that you can objectively say that foreach is clearer than map.
Did you actually bother to check the original node? Here, you compare one with the other and you decide which you think is more readable:
my $parts = [ 'Part1', 'Part2','Part3' ]; my $newLoop; for (@$parts) { my $hash = { PARTNAME => $_ }; $hash->{SELECTED}++ if $_ eq $row->{title}; push @$newLoop, $hash; } # ---- my $parts = [ 'Part1', 'Part2','Part3' ]; my $newLoop = [ map {{ PARTNAME => $_, $_ eq $row->{title} ? (SELECTED => 1) : () }} @$parts ];
Stating that foreach is clearer than map just shows your inexperience in programming techniques.
Well, I've posted lots of code here before, so I'm more than willing to let my technique speak for itself. I'll admit that I often use grep and map freely with no inhabitions at all, but not in code that ever sees the light of day. I would be less than thrilled if I was presented with a large codebase to work on full of constructs like the OP's.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: "advanced" Perl functions and maintainability
by Anonymous Monk on Dec 13, 2004 at 14:34 UTC | |
by William G. Davis (Friar) on Dec 13, 2004 at 14:58 UTC | |
by Aristotle (Chancellor) on Dec 20, 2004 at 04:36 UTC |