Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Re: Re: Think for yourself.

by tilly (Archbishop)
on Oct 06, 2003 at 03:37 UTC ( #296851=note: print w/replies, xml ) Need Help??

in reply to Re: Re: Think for yourself.
in thread is the use of map in a void context deprecated ?

Then we extract different semantic value from the same code. To me both grep and map make me start looking for where the return is going, because the return is likely to be the point of the operation. By contrast a for loop tells me that the action of the loop is what I should focus on. This bias is strong enough that while I am happy to have loops have side-effects, I will go out of my way to rewrite maps or greps with side-effect in their blocks to be regular loops instead.

If you are not the only maintainer of your code, it might be worthwhile to find out what both constructs mean to them. Remember that meaning is highly dependent on the context that the reader brings to the document. If people around you read it the same way, then the map solution may well be more maintainable in your environment. If they read it differently, then it may be better to retrain your intuition to match theirs. This kind of issue is one where having people be on the same page matters more than what page everyone is on.

Replies are listed 'Best First'.
Re: Re: Re: Re: Think for yourself.
by thelenm (Vicar) on Oct 06, 2003 at 16:37 UTC

    I agree... the use of map in a void context also makes me look for how the return value is used, because to me, getting a return list is the main reason for using map. This type of distinction reminds me of the section in perlstyle that says:

    Just because you CAN do something a particular way doesn't mean that you SHOULD do it that way. Perl is designed to give you several ways to do anything, so consider picking the most readable one. For instance

    open(FOO,$foo) || die "Can't open $foo: $!";

    is better than

    die "Can't open $foo: $!" unless open(FOO,$foo);

    because the second way hides the main point of the statement in a modifier. On the other hand

    print "Starting analysis\n" if $verbose;

    is better than

    $verbose && print "Starting analysis\n";

    because the main point isn't whether the user typed -v or not.

    To me, using map in a void context seems like "hiding" the main point of the code. And I'll point that out to people, but there are much more flameworthy transgressions. :-)

    -- Mike

    XML::Simpler does not require XML::Parser or a SAX parser. It does require File::Slurp.
    -- grantm, perldoc XML::Simpler

      Oops. I apparently did not make myself clear.

      I rarely use map or grep in a void context. When I said that I see map and grep and operations on a list. I mean it in much the same way that + is an operation on a number.

      When I need to transform a list into a new list, I tend to think of map. To me, the foreach approach focuses more on the individual elements of a list than on the list as a whole.

      That being said, I will break my own preferred approach any time it makes the code clearer.<grin/>

      G. Wade
Re: Re: Re: Re: Think for yourself.
by gwadej (Chaplain) on Oct 30, 2003 at 06:02 UTC

    I'm sorry. I appear to have added to the confusion, rather than clarifying. I should have begun by saying that I avoid using map or grep in void context.

    What I was commenting on was your suggestion that the only reason for using those operators was that they are more concise and show a better grasp of Perl.

    An example of what I was trying to say is the code example below.

    my @minutes = map { $_ / 60 } @seconds;

    In the foreach style, this would be

    my @minutes = (); foreach my $sec (@seconds) { push @minutes, ($sec / 60); }

    To me, the first signals the intent to transform a list of times in seconds into an equivalent list in minutes. The second has pretty much the same effect, but I have to read the code to see what the output is. In the first case, it seems obvious (to me, at least) that the output is the left side of the assignment.

    Although this may seem like a simple example. I find many times that I can use these operators to reduce the noise characters in code like this. Sometimes that reduction is enough to make the overall code clearer by not spending space on relative minor pieces of logic.

    Granted, if the code inside the block gets complicated enough, I may change to the foreach style to emphasize the fact that this is not a simple transformation.

    I see this as a Perl idiom that can convey information to the maintainer when used carefully. I do agree that it can be horribly misused. (The less I say about that the better.<grin type="embarassed"/>

    G. Wade
      I think that you emphasized my point. As I said, To me both grep and map make me start looking for where the return is going because the return is likely to be the point of the operation. Your code example is:
      my @minutes = map { $_ / 60 } @seconds;
      I glance at this, and indeed the return is the point of the operation. Just the words, "minutes", "map", "60" and "seconds" makes it blindingly obvious.

      However it is even more obvious because your code fits a visual pattern that I expect to see. Even a trivial code example with map or grep used for side-effects forces me to think. Compare

      map {$_ /= 60 } @times;
      $_ /= 60 for @times;
      Yeah, both are bad style. But of them, the latter is significantly easier for me to read, and not just because of length.

      On an unrelated stylistic note, if the contents of a complex mapping become complex, rather than go to a foreach loop, I am inclined to write a function for the body of the map, name it well, and insert it into the map. This emphasizes that the operation is not simple, indicates what it is, and makes it clear that the point of the code is to return something. I find this clearer, YMMV.

        I guess I've just realized that we are agreeing more than arguing.

        I had a major realization about the list operations in Perl a (relatively) short time ago, and I've been trying to explain it to others (and myself) ever since. I found that some of the problems I was working on simplified drastically when I used the list operators on lists of homogeneous data.

        I think you've helped me to understand that a lot of what I've been trying to say really boils down to another application of the Principle of Least Surprise. The places where I find the list operators to be the most useful are where they can reduce surprise to some extent.

        My example that you quoted above is a good use of map because it does what the reader expects because it follows the right visual pattern.

        The "bad" example you gave above gives the wrong kind of visual feedback and, therefore, causes more surprise. This is also code that is more likely to be corrected incorrectly, because the one who comes after may think they know what it is doing at a glance and be wrong.

        My thanks. You've given me quite a bit to ponder.

        G. Wade

        I'm not going to argue with a style judgement, style being a very personal thing, but why do you say that

        $_ /= 60 for @times;

        constitutes bad style?

        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://296851]
and cookies bake in the oven...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2018-04-24 23:20 GMT
Find Nodes?
    Voting Booth?