Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: keywords versus variables

by papidave (Pilgrim)
on Sep 08, 2007 at 16:21 UTC ( #637847=note: print w/replies, xml ) Need Help??

in reply to keywords versus variables

I'm afraid I have to vote with ambrus on this one, for most cases. The problem, as I see it, isn't the number of variables, but the distance between the definition of those variables and the location(s) where they are used. In addition to the OOP methods mentioned above, declaring your my variables in a short scope can go a long way towards improving readability.

The most obvious case would be the replacement of a single global variable with a scope-restricted variable and access methods for it. IMHO, this would be an improvement even if the code that uses those access methods needs additional temporary vars to work with it.

{ # scope of foo_tracker my $foo_count; sub found_a_foo() { ++$foo_count; } sub foo_count() { $foo_count; } sub killed_a_foo() { --$foo_count; } } # scope of foo_cache

That said, I do favor refactoring code to reduce the number of variables passed into a function, or the number of levels through which an argument is passed before it gets used.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2022-05-23 12:25 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (82 votes). Check out past polls.