Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Perl Programming guidlines/rules

by mirod (Canon)
on Nov 21, 2002 at 11:45 UTC ( [id://214744]=note: print w/replies, xml ) Need Help??


in reply to Perl Programming guidelines/rules

This looks like a good start.

I would add a couple of rules about variables:

  • Variables should be commented unless clearly self-descriptive,
  • Use a consistent variable naming style (I like lower_case_underscore_separated but some odd people prefer allInOneWordWithDanglingCaps,CONSTANT and Package_Variable can also be recommended),

And (of course!) I would argue with a couple of you rules :

14. Any reused code block above 5 lines must become a sub routine

5 lines can be a _LOT_ of code! The risk with that kind of rule is that some people might read it as "it's fine to cut'n paste up to 4 lines of code" or even "don't bother writing a sub that's less than 5 lines"

28. Do not use $_

$_ should not be abused but it has its uses and can make some blocks of code much clearer.

Replies are listed 'Best First'.
Re: Re: Perl Programming guidlines/rules
by pdcawley (Hermit) on Nov 22, 2002 at 13:20 UTC
    If you find yourself commenting your variable names then you probably didn't spend enough time thinking of a good name for it.

    Remember too that there's a difference between parameter names and variable names. In a dynamic language, a parameter name should probably suggest its type, variables should suggest their role. Kent Beck is really good on this in 'Smalltalk Best Practice Patterns'.

      Simple scalars may often have descriptive names, but I just wrote this:

      my $standard={}; # the structure that contains all the standard info # a hasref to # title => "standard title" # dir => standard directory (where the standard +page and graphics are) # des => designation (802.11a-2000) # norm_des => normalized designation (802_11_a_2000) # def => hasref { term => "html definition" }

      I found this kind of comment to be immensely useful several month later when I need to fix a bug (rare ;--) or to add features to the code (often!).

      And yes, the data represents a view of a standard, hence its name, but there are many infos that can be stored about a standard, it helps to know exactly which one this data structure is providing.

      BTW, while I agree that this structure could be an object, I don't think that the specific program I am working on requires OO. And in any case it is nice to have all the docs about the variable right where I declare it.

        Oh, I'd definitely make that an object. If only so I could either make normalized_designation a computed attributed, or at least write a set_normalized_designation that looked something like:
        sub set_normalized_designation { my $self = shift; my $designation = shift; $self->ensure_normalization($designation); $self->{normalized_designation} = shift; } sub ensure_normalization { my $self = shift; my $designation = shift; $designation =~ /.../ or die "$designation is not correctly normalized"; }
        You could certainly argue that this might be considered long winded, but writing things this way makes assumptions explicit and ensure that they're enforced.

        Of course, in Perl 6 you can have your cake and eat it:

        class Thing { my $.title is rw; my $.standard_directory is rw; # Where graphics etc are found my $.designation is rw; my Hash $.def; # Use another class? method normalized_designation { my $normalized_des = $.designation; $normalized_des =~ s/.../.../; return $normalized_des; } }
        Now, I might be a little behind on Perl 6 OO, but ISTR that setting is rw on object attributes will autogenerate accessors. If not I'm sure someone will subclass Object to do the right thing.

        Note that, in not much more space than it took to comment your hash you have an object, and, for free you get a restricted set of keys, and your dependent field removed and replaced with a computation, and you've got somewhere to hang common behaviour relating to this data structure.

        Damn, but I want Perl 6 soon.

Re: Re: Perl Programming guidlines/rules
by Mr. Muskrat (Canon) on Nov 21, 2002 at 13:11 UTC
      It's not very Perlish. The underscore style is generally preferred.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-25 03:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found