merlyn said "Both 'passing around' and 'global variables' are a signs of a misdesign....provide a higher-level interface that don't need to expose that variable...create a module..."
merlyn was explaining the Too many parameters code smell. If you are passing the same parameters around constantly from function to function, then they belong together in a single module or object. The quote that seems most appropriate is:
TooManyParameters is often a CodeSmell. If you have to pass that much data together, it could indicate the data is related in some way and wants to be encapsulated in its own class. Passing in a single structure data that belongs apart doesn't solve the problem. Rather, the idea is that things that belong together, keep together; things that belong apart, keep apart;....
That sounds like a script using local variables might require a different structure.
Not really. If you are properly scoping your variables, it would require very few changes to the structure of your script. What you would do is tease out the reusable or highly related portions of the code into separate modules.
Obviously, scoping a variable to a certain block means it shouldn't be needed outside that block (or "region"), and that takes more planning, and some luck.
Again, not really. If you notice that you are repeating yourself in your code needlessly, then separate it out. If the repeat is pretty narrow in scope, adding a new function is all that maybe needed. If its more complex, or you can use the code elsewhere, a module may be needed. This involves little planning and very little luck. Then, getting the data you need elsewhere would require a function call.
Another obvious bit of effort comes from declaring variables, and even by using "my".
Well, typing three characters ("my ") does involve some effort, but its pretty minimal. The benefits, however, in having scoped code, such as: the ability to test your code in an automated manner; reusability of code; the fact that smaller pieces of code are easier to understand, debug, and fix; etc., are much more cost effective than the alternative.
|