RefactoringImprovingTheDesignOfExistingCode seems to suggest that LocalVariablesAreBad. Are they really? Why? When are they okay?
Some local variables are bad. Some are good. Local variables in general are certainly not bad.
Local variables are bad when they are unnecessary. Example:
int foo() { int ret; ret = bletch(); return ret; }There are a few good reasons for local variables:
Theory: By eliminating local variables (or, more precisely, non-initialization assignments thereto), you approach FunctionalProgramming (which, as everyone knows, is a GoodThing (-: ). -- BillTrost
SingleAssignmentVariables? have no conflict with FunctionalProgramming, so this theory doesn't apply to them.
Yeah, the only hassle there is when you are debugging something like an interrupt handler. You can't do a lot of local variable creation and assignment because of latency issues. Oh, well.
I'd like to add that we are talking about local variables as opposed to no variables at all. Not about local variables as opposed to instance variables (which was my first idea when I read the title). In the latter case, one should make them instance variables only if they are part of the state of the object.