Localize Module Variable

Top  Previous  Next

This is pretty much the first thing to do when I am handed an unfamiliar module to fix or extend. Depending on the code, this can clean things up quite a bit - always a good way to start a new task.

 

Most of the time, you want as few module-level variables as you can get away with. There's nothing wrong with a few (and, indeed, for a class module representing an object with a lot of "state", you might have quite a few); however, too many is an indication of excessive coupling among the methods in the module. The reason that might be a problem is simply this: a module-level variable is something sitting there that any number of methods can change and use. Most of the time, it's preferable to pass parameters around instead - that way it's easier to see what's going in to and coming out of each routine.

 

Localize Module Variables helps you clean up the module-level variables in a code module. It counts how many times each is referenced. If it isn't referenced at all, CodeShine recommends that you let it delete the variable. If a variable is referenced in exactly one method, then CodeShine recommends that you move it into the method. If it is referenced in more than one method, then it recommends no action.

 

Localize Module Variable Example

Details