Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Re: Think for yourself.

by Petruchio (Vicar)
on Oct 07, 2003 at 15:07 UTC ( #297311=note: print w/ replies, xml ) Need Help??


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

And that is something I don't understand. What makes map so special? Any function in Perl will return something, be it a buildin or a user defined sub, and even operators return values. Why is it that people get all confused if they see a map in void context, and start looking where the return value goes, but they don't have problems with other functions?

What makes map so special?

Map is special, in my opinion, because there is a nearly analogous construct which does not return values.

Good style involves choosing appropriate tools. Given a situation where no return value will be used, I tend to expect someone to pick the tool which returns no value... unless there is a good reason to do otherwise. Forcing list context, for example.

If I see someone make a different choice, I will wonder why, and I will investigate. If, at the end of my investigation, I conclude that the author could have used a foreach loop just as effectively, I will be annoyed. By passing up the more appropriate (and more obvious) tool, he has obliged me to go looking for something subtle which was not there. Likewise, I wouldn't expect a person to choose

() = do_something ($_) for @some_list;

over

do_something ($_) for @some_list;

unless there was actually a need to force list context; if the author consistenly chose the former as his idiom without good reason, I'd find it very irritating (though not half so irritating as an author who inconsistently chose this, or map in void context, without good reason).

On the other hand, perhaps the author intends to alert me to the significance of context here. Saying

() = do_something ($_) for @some_list;

would then be a louder hint, and I'd generally prefer it; but if the author was consistent in using map for this purpose, I wouldn't call it bad style. I might appreciate a note somewhere, as this is not common practice.

An author might choose map for this, I think, on the grounds that he was choosing the most appropriate tool. In a very fastidious author, whose code was so clean and consistent that it gave the reader a sense of confidence that each thing was as it was for a reason, this might be a bit of minimalism which I would admire. However I would be surprised to see such a person call his own functions in such a fashion, because I wouldn't expect him to write a function which altered its arguments differently depending on the context in which it was called. I cannot think of a good reason to do this.


Comment on Re: Re: Think for yourself.
Select or Download Code
Re: Think for yourself.
by Abigail-II (Bishop) on Oct 07, 2003 at 15:24 UTC
    Good style involves choosing appropriate tools. Given a situation where no return value will be used, I tend to expect someone to pick the tool which returns no value...

    But map in void context does not return a value. No function in void context returns a value - it can't, because there's nothing to put the value in. That return values of functions are context driven is a very essential thing that makes Perl what it is. It's not map that decides whether nothing, a scalar or a list will be returned - it's the context. And map doesn't behave any different than any other function.

    By passing up the more appropriate (and more obvious) tool, he has obliged me to go looking for something subtle which was not there.
    That sounds like "for is the more obvious tool, because it's more obvious".

    Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://297311]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (14)
As of 2014-09-18 13:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (115 votes), past polls