Posted on 6/7/07 by Tim Koschützki
When your code assigns to a parameter in a function/method, use a temporary variable instead.
With assigning to a parameter I mean that when you pass in an object foo, you do not make it refer to a different object. Changing a parameter object is perfectly cool and I do that myself all the time. Just do not make it reference another object, like:
$foo = new SomeOtherClass;
The reason this is so awkward is the confusion between pass by value and pass by reference in PHP. Whereas in PHP4 everything was
passed in by value as default, PHP5 passes objects around by reference. In PHP4 when you assign the parameter another object this will not be reflected in the calling routine, which can lead to quite nasty bugs if you forget to insert the by-reference operator (&).
In PHP5 this is not so much of a problem as it passes objects around by reference anyways. So if, in PHP5, you assign to a parameter you must have a good reason anyways, because you will change the parameter object also in the calling routine with your assignment. However, I find that in PHP5 there is another area of confusion making this refactoring worthwhile for both PHP4 and PHP5. That area of confusion is within the body of the code itself. It is much clearer to use only the parameter to represent what has been passed in, because that is consistent usage and therefore a good coding practice.
It will make your code much more readable and prevents by-reference confusion and therefore big problems in the future. In PHP5, assigning to an parameter object within a method will not be reflected outside the method. So I suggest you just don't do it anyways. There seems to be no point in it anyways.
Think of hairloss. ;] Haha, I seem to be bringing up hairloss all the time in connection with bug fixing. :]
- Create a temporary variable for the parameter.
- Replace all references to the parameter, made after the assignment, to the temporary variable.
- Change the assignment to assign to the temporary variable.
- If you are on PHP5 or PHP4 (using the by-reference operator), look in the calling method to see if the parameter is used again afterward and try to understand what is changed where in your code and document that. Make sure you only return one value from the method. If more than one call-by-reference parameter is assigned to and returned, try to use a data clump or Extract Method.
Let's start with the following routine:
if ($inputVal > 50) $inputVal -= 30;
if ($quantity > 20) $inputVal -= 5;
if ($anotherFactor> 1000) $inputVal -= 2;
echo price(50, 10, 1200);
Replacing with a temp leads to:
$result = $inputVal;
if ($inputVal > 50) $result -= 30;
if ($quantity > 20) $result -= 5;
if ($anotherFactor> 1000) $result -= 2;
return $result ;
echo price(50, 10, 1200);
An easy refactoring, yet quite cool. I do it all the time. Will you, too?
You can skip to the end and add a comment.
This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.