Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Coding superstitions

by clintp (Curate)
on Jan 08, 2002 at 09:30 UTC ( [id://137055]=note: print w/replies, xml ) Need Help??


in reply to Coding superstitions

Superstitions? Hmm. After two books, countless lines of code, and training several hundred people I couldn't think of anything I did out of a fear of the supernatural. After reading the examples given, I gave it a bit more thought and came up with a few specific to Perl programming. (I have all kinds of OTHER superstitions with programming in general...) Distinguishing these from style is a bit tough. If I cross the line, excuse me. These are what I do, YMMV.

  • Trailing commas on lists, especially hashes:
    %foo=( bar => 'baz', poit => 'narf', );
  • Things to the rightleft of => I leave unquoted.
  • I only use & with subroutine calls when I intend to take a coderef (Took YEARS to break that Perl 4 habit...) coupled with always using empty parens on sub-calls I avoid nasty @_ side effects.
  • Identifiers in curlies: never quote them. I know the keywords that'll cause hell.
  • Never ever call a subroutine dump()
  • for is for for(;;) loops, foreach is for iteration over hashes and arrays and such.
  • Use extra curlies when dealing with references, even if I don't necessarily need them. (Along the lines of ${$foo}, but usually more complex).
  • Screw precedence: use parens whenever in the least amount of doubt. If I can't remember it from grade school or a common idiom, then someone else can't either.
  • Declare lexicals with my(). Yes, the parens have context effects. If context is important in a my() declaration, I've broken one of my style rules. :)
  • All statements end in semicolons, whether at the end of a block or not.
  • Explicit returns from subs. Loathe that "falling off" return value.
  • Never ever use the .0 release of any software, including Perl. Unless it's from IBM then never use the .1 release.
  • Subroutine definitions go before the code which calls them. (Pascal habit that stuck around.)
  • grep, split, join, open, close, and system always get parens. map and die do not. (Perl 4 habits)
  • The keyword "unlink" causes me to read, and re-read what I'm typing.
  • The following are all viewed with suspicion and avoided when possible: 3-arg open, locales, unicode, v-strings, overloading, and non-fork threads.
  • Never code towards anything discussed in p5p. When the feature's released -- refrain from using it until +.1 release has gone by.
  • In a regex, I don't backwhack anything that I'm not sure is a metacharacter.
  • Never name a script "test".
  • Loop counter variables always go (in order): $i, $j, $k
  • Avoid implicit $_ except in map, grep, for, or while loops that are very small.
  • Objects in their methods are always $self.
  • Quit in vi is spelled :wq
  • The 1 C-Shell command I trust is "/bin/sh"
  • #! lines always have a blank between them and code. Don't want to crowd the magic.
  • Never ever under almost any circumstances do software updates to production systems on a Friday. Never do updates on any day after 3pm.
  • Never trust the vendor's Perl. Build your own. (exception: Activestate for Win32)
  • When looping over an array, do not insert or remove things from the array. Instead push into a temp array and assign afterwards.
Now I break any/all of these when I'm trying to make a point, compress code that's just a side-show, obfuscate, etc..

Update: typo repaired, few late additions

Replies are listed 'Best First'.
Re: Re: Coding superstitions
by Juerd (Abbot) on Jan 08, 2002 at 14:12 UTC
    Things to the right of => I leave unquoted.
    I don't get it... Doesn't => force what's _left_ of it to be interpreted as a string?

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://137055]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-03-19 09:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found