Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • Method chaining is fine as long as it's clearly documented and consistent within a module. I would be hauled off to the looney bin if I were to make any kind of global recommendation, however.

    In my new Alpaca book [], I argue that for a getter/setter method, there are arguments in favor of any one of four return values from the setter method:

    • a simple true/false status (yes I was able to set this),
    • the newly set value, turning the setter into an immediate getter as well,
    • the previous value (ala umask and single-arg select), allowing flip-flop settings trivially, or
    • the object itself, allowing for chained settings.
    For anything but the first, a die has to be used to signal error.

    As long as it's documented, it's fine. Don't demand consistency. Any one of these four returns is fine.

    • Randal L. Schwartz
    • Stonehenge
  • by djberg96 (2603) on 2003.06.16 8:41 (#21106) Journal
    See Anyone who wants to support method chaining as a regular feature of their module ought to be using this. No more of this "do I return $self or a $value" nonsense.
    • That doesn't really help too much. can't distinguish between the different objects that could be wanted, getting a program to think metacognitively at that complexity is impossible. (If you've invented AI *that* good, Terminator 3 will soon become a reality...)

      I could be wanting the object returned by $object->head, because you've made the head into its own greater object to allow for more methods, or I could want the object to be a chained method. How can you tell the difference? How can a
  • Smalltalk (Score:2, Interesting)

    Smalltalk returns self by default if there is no explicit return value. I think that's part of the reason why well written Smalltalk programs look just beautiful.
  • The real problem is that you're expecting your set methods to return the value that you just set (at least in your fullname) example. So you now you have a combined get/set method, which is icky.

    If your set methods are are clearly distinct from your getters, then returning $self from setters makes perfect sense in _all_ cases.