Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Practical example of "Is Perl code maintainable"

by hossman (Prior)
on Aug 13, 2007 at 07:54 UTC ( #632154=note: print w/ replies, xml ) Need Help??


in reply to Practical example of "Is Perl code maintainable"

There is "readable" and then there is "rambling and confusing"

I would argue that from a maintainability standpoint, your version may be too terse for a novice, while the original version may be too verbose and full of noise. Might i suggest something like...

sub file_mode { my ($file) = @_; -f $file or return -1; return (stat(_))[2]; # reuse stat from -f }

...as the appropriate middle ground.

(NOTE: technically the two versions you posted aren't equivalent since yours only does one stat op and the original does two, but unless you expect the file perms to change after the "-f" test i'm guessing one stat is fine)


Comment on Re: Practical example of "Is Perl code maintainable"
Download Code
Re^2: Practical example of "Is Perl code maintainable"
by xdg (Monsignor) on Aug 13, 2007 at 10:43 UTC
    There is "readable" and then there is "rambling and confusing"

    ++

    At the cost of using a module, I'd go a bit further if readability were really the goal.

    use File::stat qw(stat); sub file_mode { my ($path) = @_; -f $path or return -1; my $stat = stat($path) or return -1; return $stat->mode; }

    That's assuming I didn't have permission to replace whatever was calling file_mode with File::stat in the first place. Something is calling file_mode, storing the result, checking for -1, etc., etc. That part is probably just as crufty.

    Update: ysth pointed out that I omitted '-f', so I re-wrote the code to be more explicit.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      Bravo, I just wanted to post the suggest for File::stat but you beat me to it. I would suggest you not return -1 for two things that could be a result of a vastly different failure, and I would instead die if I failed to stat the object. I also wouldn't import stat() as failing to include File::stat would surely result in a runtime error, and not the expected compile-time. (in addition two names for the same function has a potential to confuse, as does pollution is any form)


      Evan Carroll
      www.EvanCarroll.com
        I would suggest...

        So would I, but I was just trying to duplicate the OP's example.

        -xdg

        Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2014-12-22 06:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (111 votes), past polls