Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Object Oriented packages - classes and subroutines

by zby (Vicar)
on Sep 28, 2008 at 12:52 UTC ( [id://714162]=perlquestion: print w/replies, xml ) Need Help??

zby has asked for the wisdom of the Perl Monks concerning the following question:

Salve brethren,
When writing Perl object oriented code I sometimes leave private subroutines that are not methods (i.e. don't take the class or the object as the first parameter). When refactoring I just produce many such small subs that I don't really see as part of the interface. What do you think about that practice? Is there any danger in it? Or perhaps it is just inelegant?
  • Comment on Object Oriented packages - classes and subroutines

Replies are listed 'Best First'.
Re: Object Oriented packages - classes and subroutines
by Tanktalus (Canon) on Sep 28, 2008 at 14:01 UTC

    I often (not always) make those into methods anyway. The advantage is that others can subclass and override them. Now, you may be thinking, "But, these aren't that type of function that should be overridden." My experience says that even these can be overridden to good effect sometimes. For example, if you've pulled out a common sort function into another function, making it a method would mean that someone two years from now could decide they want a different sort order and simply override it. But you probably can't imagine why right now.

    Just food for thought ;-)

Re: Object Oriented packages - classes and subroutines
by SFLEX (Chaplain) on Sep 28, 2008 at 13:10 UTC
    That's fine. I do it myself.
    If they really want to use the subroutine they can but it shouldn't be dangerous, unless it will mess up your script.

    Spiel auf Hündinnen.
Re: Object Oriented packages - classes and subroutines
by perrin (Chancellor) on Sep 28, 2008 at 16:27 UTC
    I vote with Tanktalus: make them methods. I've had too many cases where I went back to refactor things and had to change them to methods, increasing the odds of bugs. I also have had bugs caused by not remembering which ones are methods, so I think it's easier to just be very consistent about it.
Re: Object Oriented packages - classes and subroutines
by jplindstrom (Monsignor) on Sep 28, 2008 at 22:11 UTC
    I would make them methods. Reasons for this:

    Treat them like other code. There is no reason not to. In a class, letting the routines be methods rather than functions (or whatever you want to call it) are the idiomatic norm, not the exception.

    Consistency is good on a more pragmatic level too. By having all routines being methods, you don't need to keep track of whether $self / $class should be shifted off @_ or not. You don't need to worry about which routines are called in a particular way -- they're all $self->whatever(). I find myself making this specific mistake surprisingly often when some things were made subroutines.

    The problem with not passing around $self (i.e. treating them like methods) is that sooner or later you'll need it in a routine in order to call a method on $self, and then you'll need to rewrite all of the things that call this routine, and everything that calls those routines, etc.

    If a routine is clearly not related to the object instance ($self), make it a class method (shift off $class, call the method like this: My::Class->whatever() ) instead, because...

    You almost certainly will want to override one of them in sub classes sooner or later, for the same reason you might want to override the rest of your methods. If you write a CPAN library, this is even more frustrating for other people who have needs to tweak the code that you didn't anticipate at the time.

    /J

      If a routine is clearly not related to the object instance ($self), make it a class method (shift off $class, call the method like this: My::Class->whatever() ) instead, because.. You almost certainly will want to override one of them
      This is actually really annoying as an interface, because it means that whatever can't be exported. And in my experience, for every time you want to override a class method/function, there are hundreds of times you just want to call one.
        Sorry, I don't get it. Call it like this:

        my $whatever = My::Class->whatever();

        Is it the verbosity of the class name that bothers you or something else?

        /J

Re: Object Oriented packages - classes and subroutines
by Bloodnok (Vicar) on Oct 11, 2008 at 11:07 UTC
    I'm with SFLEX on this, on the same principle as package data - if a user wants to deliberately avoid the published interface, then that's entirely their problem.

    Also, there are circumstances whereby a call of the nature &subname; has to be made - I've not yet found a way of achieving that using the method call notation .oO(Maybe that's me demonstrating, yet again, just how little knowledge of this wonderful language I posess).

    A user level that continues to overstate my experience :-))

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://714162]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-19 12:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found