Suggested feature: 'Move declaration close to first use'

A very common refactoring is to move the declaration of a variable as close to its first use as possible. The following code demonstrates:
public void MyPublicMethod( string input1, object input2 )
{
string a = @"blah" ;
int n = 5 ;
int x = 6 ;
string b = a ;
}

The declaration of a should be as closed as to where it is first used as possible.

What do you think?

Cheers,

Steve

2 comments
Comment actions Permalink

Another couple of hundred good suggestions can also be found in the book "Code Complete 2" (which is where this one came from!)

Cheers

Steve

0
Comment actions Permalink

The opposite refactoring is sometiems useful as well. Sometimes I have a
variable inside a block that I find I want or need to refer to outside the
block as I'm making changes or enhancements to existing code. Moving a
declaration outside enclosing block would be a useful tool to have as well.

"Steve Dunn" <steve.dunn@credit-suisse.com> wrote in message
news:20715368.1148302388282.JavaMail.itn@is.intellij.net...
>A very common refactoring is to move the declaration of a variable as close
>to its first use as possible. The following code demonstrates:

public void MyPublicMethod( string input1, object input2 )
{
string a = @"blah" ;
int n = 5 ;
int x = 6 ;
string b = a ;
}

>

The declaration of a should be as closed as to where it is first used as
possible.

>

What do you think?

>

Cheers,

>

Steve



0

Please sign in to leave a comment.