Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^3: Use of wantarray Considered Harmful (bad use)

by shmem (Chancellor)
on Dec 13, 2008 at 01:53 UTC ( #730094=note: print w/replies, xml ) Need Help??

in reply to Re^2: Use of wantarray Considered Harmful (bad use)
in thread Use of wantarray Considered Harmful

And yet, it is so profligate.

Of course, since it is copying all the stuff around. What about

sub x3{ 1 .. 1e6 } print time; $r = [ x3() ]; print time;


But of course that wasn't my point - I'm rather saying that it is a bad idea to hide the decision of how to deal with returns in a place far away. And no, I'm not going the "context is bad" route; but using context to obfuscate things should be used in obfuscation type of code, only...

Replies are listed 'Best First'.
Re^4: Use of wantarray Considered Harmful (bad use)
by BrowserUk (Pope) on Dec 13, 2008 at 08:55 UTC
    What about sub x3{ 1 .. 1e6 } this?

    What about it? Can you demonstrate a good use for that construct?

    I'm rather saying that it is a bad idea to hide the decision of how to deal with returns in a place far away.

    I tried hard but failed to decide which of the fallacious arguments that sentence should be categorised under. I guess that straw man is it's generic category, but it could equally be any of several others.

    One of the nicest things about Perl is that the source is almost never hidden. Failure to "use the source" and blame "a lack of documentation" for errors is laziness of the wrong kind.

    but using context to obfuscate things

    Hm. I seriously doubt that either programmer in the OPs example deliberately set out to obfuscate their code. Or that outside of Obfuscated Code, that you could find many examples of where a programmer has deliberately set out to do so.

    Whilst individual uses of context sensitive returns can be examined after the fact and declared ill-conceived, doing so in isolation of the purpose of the function itself, or the application(s) of its use, serve little useful purpose. For example, in isolation, the usefulness of:

    ... return wantarray ? @a : $a[0]; }

    seems very questionable. But if the context is:

    sub each { ... my @a = getKeyAndValue(); return wantarray ? @a : $a[0]; }

    Maybe not so much.

    And any attempt to dismiss the use of another of Perl's powerful and unique features as "bad practice" on the basis of two contrasting examples without context [sic] is a knee-jerk reaction and a premature pessimisation.

    Every one of us use one or more languages every day, that rely heavily upon context to convey meaning, and to tailor meaning to the context of use. And we learn to do this from a very early age, and without really explicitly thinking about it. The "it's too hard to learn" argument doesn't really fly in my book.

    Pick up any newspaper, magazine, technical article or transcript and pull half a dozen sentences at random from them, and examine them in isolation and it will be very difficult to understand their full meaning.

    Conversely, if we (that subset of the human species that program in Perl, and read articles on perlmonks ... (I'm giving up on that idea before I go any further:)), were to even begin to try to totally disambiguate every sentence we write, so that each sentence could stand alone in isolation, we'd fail. Because each sentence would have to run on for several pages, and have every other word be a hyperlink.

    If we cannot do it, (and wouldn't want to), in our native languages, why would we either expect or want to do it in our programming languages?

    And I find the "creates the potential for errors during refactoring" argument equally weak. If a function returns a reference and the refactorer fails to dereference it before appending it to a string, he's made a error, plain and simple. That does not form a salient argument for the abolition of references.

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://730094]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2018-06-20 02:52 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.