in reply to Practical example of "Is Perl code maintainable"
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:
- Never use implicit shift; always specify @_ as in shift @_.
- Don't use logical operators in place of real flow control.
- Don't hide flow control off to the right of a statement.
- Don't write code that looks like it would be tripped up by Perl's "looks like a function" rule.
- Avoid magic numbers like "2".
- 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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Practical example of "Is Perl code maintainable" (golf)
by eyepopslikeamosquito (Archbishop) on Aug 13, 2007 at 21:14 UTC | |
Re^2: Practical example of "Is Perl code maintainable" (golf)
by xdg (Monsignor) on Aug 13, 2007 at 18:52 UTC | |
by tye (Sage) on Aug 14, 2007 at 02:39 UTC | |
Re^2: Practical example of "Is Perl code maintainable" (golf)
by eyepopslikeamosquito (Archbishop) on Aug 13, 2007 at 20:32 UTC |