Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

I hate the leading underscores.

by Anonymous Monk
on Feb 16, 2005 at 09:08 UTC ( [id://431478]=perlmeditation: print w/replies, xml ) Need Help??

Does anyone else hate the way method names with leading underscores look? I understand that it's used to differentiate between public methods, those that makeup the interface, and private methods, those that are internal and never meant to be invoked by users of your code, but there have to be better alternatives. The underscore is especially bad if you use CamelCase:

sub _somePrivateMethod { my $self = shift; $self->_someOtherPrivateMethod; ... }

What about dropping the underscores and instead putting all private methods together after the public methods, with a comment block indicating that the following methods are private?

What do you guys think of this? What do you like to use to distinguish between the public and private parts of your modules?

Replies are listed 'Best First'.
Re: I hate the leading underscores.
by cog (Parson) on Feb 16, 2005 at 10:02 UTC
    Personally, I like the leading underscores. What I don't like is CamelCase... instead of _somePrivateMethod, I'd use _some_private_method.

    Putting all private methods together after the public methods would be just nonsense. What's easier to read, alternative 1:

    sub function1 { #... } sub _aux_function1 { #... } sub function2 { #... } sub _aux_function2 { #... } sub function3 { #... } sub _aux_function3 { #... }

    Or alternative 2:

    sub function1 { #... } sub function2 { #... } sub function3 { #... } sub _aux_function1 { #... } sub _aux_function2 { #... } sub _aux_function3 { #... }

    And if you're still not convinced, try with a larger example, preferably with pod in the middle.

Re: I hate the leading underscores.
by blazar (Canon) on Feb 16, 2005 at 09:22 UTC
    Does anyone else hate the way method names with leading underscores look?
    Personally I don't, but then again it's just a matter of personal preference. Just the other day I wrote some code that used a dispatch table and since the involved subs were not really trivial nor "minimal" I wrote them separately as named subs and put in the table references to them. Even if they were not methods of a class, since they were coneceptually somewhat "private" to the table, I used names with leading underscores for them. I find it quite natural...

    UPDATE: In an even different situation in which I want a "private portion of a namespace" since I'm only using it as an "anonymous tool" I still use those leading underscores, e.g.:

    sub is { return bless \shift, '_hidden'; } sub _hidden::in { my $s=${shift,}; $s eq $_ and return 1 for @_; 0; }

      You mean instead of

      my %dispatch; $dispatch{foo} = sub { ... }; $dispatch{bar} = sub { ... }; ...
      ?-)

      Jenda
      We'd like to help you learn to help yourself
      Look around you, all you see are sympathetic eyes
      Stroll around the grounds until you feel at home
         -- P. Simon in Mrs. Robinson

        I mean like this:
        my %dispatch=(FOO => \&_foo, BAR => \&_bar, # ... LAST => \&_last); # And at the end of the script: sub _foo { # ... } sub _bar { # ... } # ...
        (Yes, I may have done as suggested by you instead.)
Re: I hate the leading underscores.
by Mutant (Priest) on Feb 16, 2005 at 11:23 UTC
    I don't have a problem with leading underscores. I'd rather have them as a convention than trying to guess at how someone else has decided to do it. (And they are *just* a convention, I like being able to call private methods publicly. Sometimes and assumption you made at design time (e.g. "it doesn't make sense for anyone outside of me to call this method") proves to be wrong.)
      That means, you give up encapsulation. If you allow private methods to be called, they aren't private, and if you change the interface or functionality of such a method, or remove it all together, you've just broken backwards compatability.

      OO without encapsulation doesn't make any sense to me. Then it just becomes syntactical sugar to be able to use cool looking arrows.

        OO without encapsulation doesn't make any sense to me. Then it just becomes syntactical sugar to be able to use cool looking arrows.

        Many, I amongst them, would argue that your statement is incorrect. You have used the word "encapsulation" where, from the context, you really meant "information hiding".

        That may seem like a facetious argument, but this Encapsulation is not information hiding may change your mind.


        Examine what is said, not who speaks.
        Silence betokens consent.
        Love the truth but pardon error.
        Agreed. Private methods/variables should have no reason to be called outside a class. If necessary, simply make a public accessor that calls that private method/variable.

        I personally use underscores because it is helpful from a visual standpoint. Especially when there are no built-in security measures in place to hide variables and methods (not include closures, but that's not what I'd consider a "built-in" way to hide things, no offense to closures, of course).

        Even still, coming from Java, I used them there. You simply get used to it, and while I agree it is not the prettiest thing in the world, I've seen worse :)
        Howdy!

        That means, you give up encapsulation. If you allow private methods to be called, they aren't private

        The first claim is non sequitur. The second depends on a specific connotation of "private" that does not necessarily hold. Further, the concept of "allowing" methods to be called implies some way to prevent it, which is decidedly non-Perlish.

        Moving onward, you can troll through the namespace of a module to see what CODE references you can find in the name space. That then gives you names of subroutines. If you want to actively prevent methods from being called under certain circumstances, you can use caller() to try to decide if the caller is acceptable, and barf otherwise. In general, that is a lot of work for little to no gain. Further, that sort of "protection" can still be circumvented by clever hackery -- hackery that has no place in a "finished" product, but possible hackery nonetheless.

        yours,
        Michael
Re: I hate the leading underscores.
by edan (Curate) on Feb 16, 2005 at 14:36 UTC

    What do you like to use to distinguish between the public and private parts of your modules?

    Heh. Heh-heh. He said "private parts".

    --
    edan

Re: I hate the leading underscores.
by Anonymous Monk on Feb 16, 2005 at 10:56 UTC
    I don't use leading underscores. I think it's pretty simple. If it's a public method, it's part of the API. If it's part of the API, it's documented in the POD. Also, if it's documented in the POD, it's part of the API. And therefore, it's a public method. From which follows that no private method is part of the API, and no private method is mentioned in the POD. And any method that isn't mentioned in the POD is a private function.

    It all boils down to encapsulation. With proper encapsulation (and documentation), users never need to look at the source to get their programs working correctly. Document well, encapsulate well, and you don't need "leading underscore" conventions (doesn't that smell awfully Hungarian?).

    This implies also that I don't use POD::Coverage.

      This implies also that I don't use POD::Coverage.

      There is no reason you cannot still use Pod::Coverage. All you need to do is to tell it the methods which you decide are private, and it will skip them. You basically just pass in one (or more) of these options to the constructor:

      private an array of regexen which define what symbols are regarded as private (and so need not be documented)

      also_private items are appended to the private list

      trustme an array of regexen which define what symbols you just want us to assume are properly documented even if we can't find any docs for them

      And of course all these options are available thougth Test::Pod::Coverage as well.

      -stvn

      That's sort of what I do too. Give the public methods pod and just give the private methods comments. I'd like to just go with that and drop the underscores.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://431478]
Approved by Corion
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-25 19:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found