Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Reading your proposed code, I'd think you were shooting for a golf win rather than for maintainable code. I think you make a good case for the bad influence of "golf" on the ability of Perl coders to write maintainable code, which I'm certain is not something you want to do.

Here are some of your infractions against my personal list of best practices for Perl:

  1. Never use implicit shift; always specify @_ as in shift @_.
  2. Don't use logical operators in place of real flow control.
  3. Don't hide flow control off to the right of a statement.
  4. Don't write code that looks like it would be tripped up by Perl's "looks like a function" rule.
  5. Avoid magic numbers like "2".
  6. Use meaningful variable names (having no variable name means it can't be meaningful).

The original code is a bit verbose and demonstrates a lack of knowledge about list slices and a lack of confidence about list assignment in Perl. I'd certainly chop all of the trailing $dummys since that just adds noise. The rest of the code is at least acceptable and most of it I much prefer over your golfed version.

The most maintainable code I'd write would look more like:

sub file_mode { my( $fileName )= @_; if( ! -f $fileName ) { return -1; } my( $dev, $ino, $mode )= stat( _ ); return $mode; }

or maybe

# The items returned by stat(): my( @Stat, %Stat ); BEGIN { @Stat= qw( dev ino mode nlink uid gid rdev size atime mtime ctime blksize blocks ); %Stat{@Stat}= ( 0 .. $#Stat ); } sub file_mode { my( $fileName )= @_; if( ! -f $fileName ) { return -1; } my $mode= ( stat _ )[ $Stat{mode} ]; return $mode; }

Note that I didn't use File::stat because the BUGS section is unacceptable to me. ETOOMUCHMAGIC is such a common problem. I wish more Perl coders could learn to be proud of writing simple code, rather than so often nearly trying to invent a new language with each module in the apparent attempt to provide one's personal "perfect syntax" for expressing what the module is meant to address. It would have been better for File::stat to avoid overriding the name stat().

- tye        


In reply to Re: Practical example of "Is Perl code maintainable" (golf) by tye
in thread Practical example of "Is Perl code maintainable" by eyepopslikeamosquito

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-26 08:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found