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

Re: shift vs @_

by tomhukins (Curate)
on Oct 02, 2006 at 20:11 UTC ( #575946=note: print w/ replies, xml ) Need Help??


in reply to shift vs @_

I can think of one reason not yet mentioned: Multi-line shift makes patches more readable. Imagine you have:

sub blah { my $foo = shift; my $bar = shift; }

If I want to add another argument (maybe at this point I should consider hash-based arguments as above, but let's assume I can't, maybe for backwards compatibility), I might rewrite this as:

sub blah { my $foo = shift; my $bar = shift; my $baz = shift; }

The patch to add this argument only contains one additional line with the new argument, making the code easier for people reviewing changes in your version control system or reviewing patches before committing them.

If you change my ($foo, $bar) = @_; to my ($foo, $bar, $baz) = @_; traditional patch/diff output shows one line removed and one added which makes it a little less clear that the change does nothing other than add one new argument.


Comment on Re: shift vs @_
Select or Download Code
Re^2: shift vs @_
by Argel (Prior) on Oct 02, 2006 at 20:26 UTC
    I'm not going to claim this is better but it does allow you to meet your change management goals while still using the my()=@_ form.
    sub blah { my( $foo, $bar, $baz, ) = @_; }
      Actually, I think I will claim it is better. In theory it should be easier to notice changes since there is less clutter -- no leading "my" and no trailing "= shift" that someone eyeballing the code has to filter out so the variables themselves should stand out better.
        Perhaps, but consider the starting point of one argument:
        my( $foo, ) = @_;
        compared to:
        my $foo = shift;
        I use the shift approach because it gives me a clean starting point and lets me extend my methods (or subroutines) easily. Having said that, I try to avoid extending public methods and prefer to create new methods when appropriate. But perhaps I'm drifting away from the original question a little..
Re^2: shift vs @_
by GrandFather (Cardinal) on Oct 02, 2006 at 21:25 UTC

    That depends on the diff tool you use. TortoiseSVN's diff shows changes within a line for example, so the extra verbiage associated with the shift is not required. Besides, generally the number of times you have to check deltas to solve a problem are many fewer than the number of times you look at the code in the process of writing or using it. An idiom that provides clarity in writing and use is more important than occasional merge or delta issues.

    Using the list assignment form makes it easier to match the parameter list in the call with the required parameters - the assignment list self documents the required parameters (especially if good names are used).


    DWIM is Perl's answer to Gödel

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2014-08-23 04:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (172 votes), past polls