Often, when I am initializing something I have to use a temporary variable, for example:file_str = path/to/filefile_file = open(file)orregexp_parts = ['foo', 'bar']regexp       = new RegExp( regexp_parts.join('|') )However, I like to reduce the scope my variables to the smallest scope possible so there is less places where they can be (mis-)used. For example, I try to use for(var i ...) in C++ so the loop variable is confined to the loop body.In these initialization cases, if I am using a dynamic language, I am then often tempted to reuse the same variable in order to prevent the initial (and now useless) value from being used latter in the function.file = path/to/filefile = open(file)regexp = ['...', '...']regexp = new RegExp( regexp.join('|') )The idea is that by reducing the number of variables in scope I reduce the chances to misuse them. However this sometimes makes the variable names look a little weird, as in the first example, where file refers to a filename.I think perhaps this would be a non issue if I could use non-nested scopesbegin scope1   filename = ...begin scope2   file = open(filename)end scope1   //use file here   //can't use filename on accidentend scope2but I can't think of any programming language that supports this.What rules of thumb should I use in this situation?When is it best to reuse the variable?When is it best to create an extra variable?What other ways do we solve this scope problem?
Is it OK to use dynamic typing to reduce the amount of variables in scope?
programming languages;code quality;variables;scope
Short answer: No, don't repurpose variables.The idea is that by reducing the number of variables in scope I reduce  the chances to misuse them.It sounds more like you're misusing variables so that you can reduce the number of them.Variables are cheap -- use as many as you need. Reusing variables to represent different things at different times will not be cheap in the long run; it will make your code more difficult to understand, and you or someone after you will be much more likely to create bugs trying to maintain such code.What other ways do we solve this scope problem?The best way is to choose descriptive names for your variables and never try to reuse one variable for two or more different concepts. The problem is not scope, the problem (as far as I can tell) is accidentally using a variable for the wrong thing. Descriptive names help in that respect.You can always set a variable that you no longer need to a value that's harmless or invalid. This is common in languages with manual resource management, where you free/delete/release pointers when you're done with them. After doing so, it's often a good idea to set them to nil to prevent future use of that pointer. You can do similar things with other types, like setting a loop counter to -1, but in practice it's not usually necessary.Also, don't write functions/methods that are so large that you have a hard time keeping track of all the variables you're using. If you've got dozens of variables floating around, there's a good chance that you're code is too complicated; break it down into smaller independent tasks.