Created
October 16, 2018 00:40
-
-
Save SqrtNegInf/df5fbab044babc6ca16e229d2d1c669f to your computer and use it in GitHub Desktop.
Bad naming practices, illustrated
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
## Some programs exist merely to serve as an object lesson to others... | |
package Foo { | |
our $F = qq{"Life, the Universe, and Everything" by DA}; | |
our $O = 'Off by one error'; | |
} | |
$f = ' is'; | |
$o = ' the'; | |
$o_= ' answer'; | |
$foo = &foo($Foo::F); | |
push @foo, --$foo; $foo++; | |
push @foo, ++$foo; $foo--; | |
$foo{$foo} = $Foo::F; | |
$foo{$foo[$#foo]} = $Foo::O; | |
$foo{$foo[$foo ne $foo]} = $Foo::O; | |
sub foo { split '', shift } | |
$foo_ .= "$foo$f$o$o_\n"; | |
$foo_ .= $foo{$foo[$foo/$foo]} . "\n"; | |
$foo_ .= $foo{$foo[$foo-$foo]} . "\n"; | |
$foo_ .= $foo{$foo} . "\n"; | |
print $foo_; | |
my $FOO = <<'FOO'; | |
42 is the answer | |
Off by one error | |
Off by one error | |
"Life, the Universe, and Everything" by DA | |
FOO | |
use Test::More; | |
is ($foo_, $FOO); | |
done_testing(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Verbiage from Rosetta-Code 'Naming_conventions' task: | |
This being Perl, you are of course given wide lattitude in the names you can use for your program elements. | |
But it can be helpful to other programmers, and possibly your future self, to use letter case and underscores | |
to indicate the scope or nature of a variable, so their roles can be understood at a glance. | |
Some generally accepted conventions: | |
$ALL_CAPS_HERE constants | |
$Some_Caps_Here package-wide global/static | |
$no_caps_here function scope my/our/local variables | |
Note the use of underscores for readability. | |
Subroutines and variables meant to be treated as private are prefixed with an underscore. | |
With all-caps constants, be careful for conflicts with Perl special variables. | |
Function and method names should be all lowercase. | |
Any reasonably modern version of Perl can use Unicode characters in names, so employ them where it makes sense. It | |
may be necessary to invoke the <code>use utf8</code> pragma such cases. | |
<b>A caution about variables names and sigils</b>: | |
Perl won't stop you from giving three variables in the same scope the names | |
<code>%foo</code>, <code>@foo</code> and <code>$foo</code>, but that doesn't | |
mean it's a good idea. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment