Composing Methods: Split Temporary Variable
Posted on 2/7/07 by Tim Koschützki
When you have the same temporary variable assigned to more than once, split it up into two, unless it is a loop variable.
Temporary variables have various uses. They can be used as counters in loops, as collecting variables building up a result or as simple containers containing the result of a long-winded expression for easy reference.
These kinds of variables (containers) should be assigned to only once. Else one could think they have more than one responsibility, resulting in your code being less readable. Using a temp for two things is very confusing for the reader. Introduce a new temp for every new thing.
Don't listen to the performance people who say that introducing another variable will use up more memory unnecessarily. Optimize your application where it is having performance bottlenecks and don't believe such silly crap. Registering a new variable within your memory will not eat it up - especially not these days where at least one Gig of RAM is common among the webhosters.
Readable code will lead you to refactorings that will improve upon the application's speed enormously anyway. If you hadn't done the refactoring you would have never gotten the chance of seeing that performance bottleneck in the first place.
- Change the name of a temp at its declaration and its first assignment. Don't change collecting or building variables (like $i = $i + some expression; $i .= some expression, etc.).
- Change all references of the temp up to its second assignment.
- Declare and name a new temp at the second assignment of the old temp.
- Repeat the stages, renaming at the declaration and changing references until the next assignment.
When calculating two things for the same object, having the same temp for both calculations is quite common. Let's look at a possible scenario for a rectangle and fix it up:
$temp = 2 * ($width + $height);
echo "Perimter: $temp
$temp = $width * $height;
echo "Area: $temp";
As you see, $temp is used for both the perimeter and the area computation. It is still readable within that example, but imagine slightly more complicated code. Wouldn't you want the code to look something like the following then instead?
$perimeter = 2 * ($width + $height);
echo "Perimter: $perimeter
$area = $width * $height;
echo "Area: $area";
Always introduce a new temp for a different thing. Performance most often is not an issue. Make your code readable, please.
You can skip to the end and add a comment.
I always use original variable names, this makes it easy to modify the code when you come back to it a few months or years later.
It depends, sometimes i do it in one way, and sometimes in completly different one.
Depending on what?
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.
[...] the PHP-Coding-Practices.com blog, there’s a tutorial posted from Tim Koschuetzki in his “Composing Methods” series looking at assigning temporary [...]