Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: How to best pass on the context / wantarray?

by GrandFather (Sage)
on Mar 26, 2006 at 18:27 UTC ( #539305=note: print w/replies, xml ) Need Help??

in reply to How to best pass on the context / wantarray?

This, on the face of it, seems to be a Bad Idea™. Cute, but bad. There may be situations where it makes sense to suborn a verb to report a status depending on calling context, but this is not a good example. If I see a sub called move_xxx I expect movement to happen. The implication here is that the calling object is updated or not depending on calling context, and as we know calling context can be rather subtle at times! What is the context in the following case for example?

get_wibble_cursor ()->move_xxx ()

Although it has nothing like the cuteness factor, simply using a differently named method makes the code both easier to write and to understand, and probably shorter too. In this case move_xxx and moved_xxx would be good pairs.

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: How to best pass on the context / wantarray?
by Corion (Pope) on Mar 26, 2006 at 18:31 UTC

    I'm not yet set on ->move_left and ->move_right - these two will maybe just become ->left and ->right, so the expectation of movement vs. result gets more ambigous again.

    The idea of using move and moved isn't bad, but is a bit too wordy for my taste. I could create the move version of a moved method automatically, so I'll keep that in mind as the alternative route to take when my dual-life idea doesn't work out. One thing that speaks for your approach is, that your approach is easily translated to JavaScript while I'm not aware of anything like wantarray in JavaScript.

      This sort of issue arrises often with respect to overloading of functions in languages like C++. In general the name of the function should say it all so that when you are reading the code you don't have to go looking elsewhere to discover subtle, but important, details about the parameters and return results.

      However this can lead to "identifiers that tell a story" which is even worse - hard to type and hard to read. There is a real art to creating good identifiers!

      The other aspect of this is a tension between a rich interface and a lean interface. A rich interface gets harder to use and to maintain. A lean interface tends toward ambiguity and missing functionality. This tension also plays part in the art of programming.

      DWIM is Perl's answer to Gödel

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://539305]
What's the matter? Cat got your tongue?...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2018-01-21 10:59 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (227 votes). Check out past polls.