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. | [reply] [d/l] [select] |
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;
}
| [reply] [d/l] |
|
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 |
| [reply] [d/l] |
|
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.) | [reply] [d/l] |
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.) | [reply] |
|
| [reply] |
|
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.
| [reply] |
|
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 :)
| [reply] |
|
|
|
|
|
|
|
|
|
|
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.
| [reply] |
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".
| [reply] |
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. | [reply] |
|
| [reply] |
|
| [reply] |