Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I agree with tye - does look like golf :)

There comes a time when you have to balance this stuff out in a shared environment. For example, I've been optimising stuff perhaps too far:)

sub set_attr { # 0 self # 1 attr # 2 value $_[0]->{$_[1]} = $_[2]; } sub get_attr { # 0 self # 1 attr $_[0]->{$_[1]} }

What has come from this though is that I'm moving towards a more consistant @_ usage, and then passing the script through a source filter before publishing. IE, if the sub doesn't amend any arguments unexpectedly, I write code that shifts the args

sub set_attr { my $self = shift; my $attr = shift; my $value = shift; $self->{$attr} = $value; }

but, before publishing, I run the code through a source filter to change it to:

sub addNums { # 0 self # 1 attr # 2 value $_[0]->{$_[1]} = $_[2]; }
If the source filter may cause confusion by amending any of the arguments, I use @_ explicitly:

sub capitalize { my ($word) = @_; $word = "\u$word"; return $word; }

I personally like using subs that tweak args direct:

sub capitalize { $_[0]="\u$_[0]"; }

But I find that most people feel more comfortable with:

$word = captitalize($word);

over

captitalize($word);

I'm sure there are some disadvantages to my approach, but as long as I keep it consistent, document the source filter and ensure I run tests before and after source filter is applied, I think I'm OK :)


In reply to Re: Practical example of "Is Perl code maintainable" by cLive ;-)
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 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? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2021-08-01 01:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?