Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

(tye)Re: Shift versus Sanity

by tye (Sage)
on Apr 24, 2002 at 04:18 UTC ( #161513=note: print w/replies, xml ) Need Help??

in reply to Shift versus Sanity

If the function takes a simple list of arguments with perhaps some trailing arguments being optional, then I will often write:

sub simple { my( $this, $that, $other )= @_;
just because it is simple, takes up little space, and is easy to read.

But if my argument handling is more advanced, or I find myself changing what arguments the function accepts, or I feel a need to add comments, or for probably quite a few other reasons, I will instead use something much closer to:

sub complex { my $this= shift(@_); my $that= shift(@_); my $other= shift(@_);
because it is much easier to make changes to.

Note that I don't use a bare shift mostly because I really like to be able to scan for just "@_" in order to see where any argument handling is happening. I don't want to scan for that plus "shift", "pop", $_[, and several others. I also like that it makes the code a bit easier for non-Perl programmers to read (which makes it easier to have the code accepted by coworkers and managers) and clearly documents that I didn't write the code thinking I was dealing with @ARGV and then later moved it into a subroutine and broke it.

I also use the asymetrical spacing around the assigment operator to make it clearer that I didn't mean to write == (no, that isn't a likely source of confusion for this code, but you have to follow that convention for all assignments for it to work well).

And I don't line up the expressions like:

sub pretty { my $first= shift(@_); my $second= shift(@_); my $optional= shift(@_);
as I think this scales really poorly when you decide to rename some variable and suddenly you feel obliged to reindent a bunch of nearby parts of lines, especially since no editor I've seen comes even close to automating or even assisting much in such primping.

And I certainly don't prematurely nano-optimize for run-time speed since development time is usually much more important and run-time speed is usually much more improved by careful algorithm design than such pre-benchmarking.


        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: (tye)Re: Shift versus Sanity
by herveus (Parson) on Apr 24, 2002 at 18:43 UTC

    perltidy will gladly re-align those assignments for you. I've got it integrated into my favorite text editor (under Solaris -- nedit) to do the whole file or the selection.

    Mind you, I'm not real fussy about that detail, myself.

    ObThread: for OO stuff, I start with my $self = shift; and go from there. Sometimes I capture @_; other times I do more shift-ing. It depends...


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2021-10-19 00:27 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (76 votes). Check out past polls.