<?xml version="1.0" encoding="windows-1252"?>
<node id="464950" title="coding rules" created="2005-06-09 00:18:24" updated="2005-08-15 08:57:16">
<type id="120">
perlmeditation</type>
<author id="231169">
punkish</author>
<data>
<field name="doctext">
&lt;b&gt;Update:&lt;/b&gt; &lt;i&gt;Several rules now modified to reflect the feedback from wise monks (credited, hopefully, correctly, below the respective rule).&lt;/i&gt;
&lt;hr&gt;
Larry's philosophy that different things should look different is one of the most beautiful and powerful strengths of Perl. We are immediately guided by the $, @, and % as to the nature of the variable (cr: [TimToady]).
&lt;p&gt;
However, for my purpose, it can go even further. With that in mind, I came up with the following &lt;strike&gt;9&lt;/strike&gt; 10 rules while coding a reasonably complicated project.
&lt;p&gt;
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 --
&lt;p&gt;
&lt;code&gt;
# 0. Just-in-time variable declaration and initialization.
&lt;/code&gt;
(cr: [merlyn])
&lt;code&gt;
# 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
);
&lt;/code&gt;

&lt;strike&gt;
&lt;pre&gt;
# 4. Lexicals in all lower case
my ($foo, $bar, $baz, @foo_bar);
&lt;/pre&gt;
&lt;/strike&gt;

&lt;code&gt;
# 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);
&lt;/code&gt;

&lt;strike&gt;
&lt;pre&gt;
# 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;
}
&lt;/pre&gt;
&lt;/strike&gt;

&lt;code&gt;
# 5. Local lexicals on all lowercase
sub foo {
	my ($bar) = @_;
	for (@$bar) {
		print "yay" if ($_ eq $FOO);
	}
	return $bar;
}
&lt;/code&gt;
#4 and #5 above flipped  (cr: [Forsaken])&lt;/br&gt;

&lt;code&gt;
# 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-&gt;param('foo');
my $cgi_bar = $cgi-&gt;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.
&lt;/code&gt;

&lt;strike&gt;
&lt;pre&gt;
# 8. Subroutines named in camelCase with the first letter 
#    lower case
sub normalizeThisMonkeyBoy {
}
&lt;/pre&gt;
&lt;/strike&gt;

&lt;code&gt;
# 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
&lt;/code&gt;
(cr: [TilRMan])&lt;br&gt;
&lt;code&gt;
# 10. Always pass named vars to subs
doThis('with' =&gt; $that, 'and' = $something_else);
&lt;/code&gt;

&lt;div class="pmsig"&gt;&lt;div class="pmsig-231169"&gt;
--
&lt;br&gt;&lt;br&gt;&lt;i&gt;
when small people start casting long shadows, it is time to go to bed&lt;/i&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;small&gt;Janitored by [holli] - moved to [Meditations]&lt;/small&gt;&lt;/p&gt;</field>
</data>
</node>
