Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Refactoring Perl #7 - Remove Assignments to Parameters

by hossman (Prior)
on Aug 23, 2007 at 20:39 UTC ( [id://634728]=note: print w/replies, xml ) Need Help??

in reply to Refactoring Perl #7 - Remove Assignments to Parameters

even after your update, you probably still want to change the main example in your post ... the initial code black already has the param assigned to a temp variable in the my $arg_ref = shift; line and demonstrates the spirit of the refactoring -- it should be your "post refactoring" example. Your "pre refactoring" example should have every line modifying $_[0] directly.

Replies are listed 'Best First'.
Re^2: Refactoring Perl #7 - Remove Assignments to Parameters
by agianni (Hermit) on Aug 24, 2007 at 12:51 UTC
    Actually, I think the example code as it stands is in the spirit if not the word of what the refactoring pattern is meant to avoid. While $arg_ref is a local temp, it's still a reference to the original arguments (a hashref), so updating it would update the arguments pass, which is what this pattern is meant to address. Hence the introduction of the secondary temp. That said, for the sake of completeness, the completely un-refactored code might look like this as you suggest:
    sub discount{ $_[0]->{input_val} -= $_[0]->{input_val} > 50 ? 2 : 0; $_[0]->{input_val} -= $_[0]->{quantity} > 100 ? 1 : 0; $_[0]->{input_val} -= $_[0]->{year_to_date} > 10000 ? 4 : 0; return $_[0]->{input_val}; }

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 06:05 GMT
Find Nodes?
    Voting Booth?

    No recent polls found