However, for my purpose, it can go even further. With that in mind, I came up with the following 9 10 rules while coding a reasonably complicated project.
Coding strategies are personal by nature, but much can be learned from those who are wiser. Hence, I put forth my rules to get comments from the more learned monks than I --
(cr: merlyn)# 0. Just-in-time variable declaration and initialization.
# 1. Variables in consistent case (all lower or all upper) # 2. Each dictionary word in the variable name separated # by underscore, unless accepted as one word by usage as # in 'username' or 'logout' # 3. Package variables in all caps, used for things like # config values use vars qw( $FOO $BAR $BAZ $FOO_BAR );
# 4. Lexicals in all lower case my ($foo, $bar, $baz, @foo_bar);
# 4. Global lexicals in all lower case, and # prefixed with some text to mark them as such. # For example... my ($glob_foo, $glob_bar, $glob_baz, @glob_foo_bar);
# 5. Variables local to a subroutine prefixed by some
# consistent prefix such as 'this_' or 'local_'. This way
# there never will be any confusion as to whether a
# given variable sprang into life in the subroutine
# of if it exists in the outside world and might possibly
# get clobbered locally.
sub foo {
my ($this_bar) = @_;
for (@$this_bar) {
print "yay" if ($_ eq $FOO);
}
return $this_bar;
}
#4 and #5 above flipped (cr: Forsaken)# 5. Local lexicals on all lowercase sub foo { my ($bar) = @_; for (@$bar) { print "yay" if ($_ eq $FOO); } return $bar; }
# 5.5. Use prefixes to scope other logically different # vars. For example, 'cgi_' to prefix vars holding # params delivered via the cgi my $cgi_foo = $cgi->param('foo'); my $cgi_bar = $cgi->param('bar'); # 6. Refs prefixed with the appropriate identifier. I wish # Perl would extend the "different things look different" # to enable easy identification of the kind of data # structure a ref refers to. my $aref_bar = foo(\@foo_bar); # 7. Rules 1-5 apply to rule 6 as well... so, an array ref # inside a sub would be $this_aref_bar, etc.
# 8. Subroutines named in camelCase with the first letter
# lower case
sub normalizeThisMonkeyBoy {
}
(cr: TilRMan)# 8. modified: I like camelCase, however, the real purpose # here is to visually distinguish a sub from a var. Choose # your method # 9. Subroutines not exported from a package prefixed with # an underscore package Foo; @EXPORT = qw( startProgram watchOut goHome ); sub startProgram { _wakeUp(); } sub watchOut { _keepEyeOpen(); } sub goHome { _nightyNight(); } sub _wakeUp {} sub _keepEyeOpen {} sub _nightyNight {} # 9.5. Never export anything by default. @EXPORT_OK
# 10. Always pass named vars to subs doThis('with' => $that, 'and' = $something_else);
Janitored by holli - moved to Meditations
In reply to coding rules by punkish
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |