I'm under the impression that this is generally a good idea, as long as you use the same variable name for the same thing all the time.
For example, the code of the IRC bot I'm working on (yeah yeah, I know, yet another IRC bot) is really littered with $user and $channel variables. All in different sections of the code and in different subroutines.
$user holds the nickname of the user who is responsible for a given event (IE, the user who is talking, the user who changes a mode somewhere, the user who gives channel operator status to someone, and so on). In linguistics, this is often called the agent. (On a sidenode: whenever there is a patient, I usually store his name in $who. A patient is the user who, say, recieves operator status)
$channel holds the channel name of where this event occurs.
The bot of course keeps track of which users are in what channels.
But recently I had to revise the subroutine that handles KICK events. I used $user there too. But to my suprise I discovered the following (paraphrased) code: delete $channelDB{$channel}->{$user}
Wait, what? Am I really removing the agent from the list? That's not supposed to happen! I of course want to remove the one who's being kicked, the patient. So I scrolled up in the code a bit to see in what variable I was storing the patient's username. Funnily enough, it turned out I was right with my delete $channelDB{$channel}->{$user} after all: I stored the patient in $user!
That, of course, is a Bad Thing. In every other subroutine (and let me tell you there are quite some subs in this whole thing), $user refers to the agent, not to te patient. In this case, I stored the agent in $kicker (which made me giggle when I saw that, because kicker sounds very similar to Dutch kikker, which means frog. Anyway...).
So. To get back at my point: re-using variable names in different parts of the code is okay, as long as one variable name always holds the same kind of value, in my opinion. That way you'll always be able to see that delete $channelDB{$channel}->{$user} removes the agent of an event from the list of users of the channel where that event occurs.
Of course, when I was revising the code for the KICK handler anyway, I also fixed it. $kicker was renamed $user, and what was $user is now $who. So whenever I need to change that subroutine again, I just know that $user kicks $who from $channel.
Edit: fixed a typo
In reply to Re: Confusion in naming variables in subroutines
by muba
in thread Confusion in naming variables in subroutines
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |