当前位置:编程学习 > 网站相关 >>

Replace Assignment with Initialization -- 以初始化值取代赋值

Replace Assignment with Initialization
Refactoring contributed by Mats Henricson
You have code that first declares a variable and then assigns a value to it
你有段代码首先声明了一个变量,然后才去进行赋值。
Make it into a direct initialization instead
以初始化值取代赋值
void foo() {
   int i;
   // ....
   i = 7;
}
 
void foo() {
   // ...
   int i = 7;
}
Motivation
You often see functions in which the programmer has first declared lots of variables that will be used in that function, and then, further down the function, assigns values to them. This is often an artifact from older programming languages, where you had to declare in the beginning of the function all variables that are used in it. In C++ and Java this is not necessary, and in C++ this old style of programming can give you slower programs, since a declaration of a variable in C++ often means a call to the default constructor. In Java, declarations of variables are cheap, if not even free, but the problem is that you end up with unnecessary lines of code that adds no value to your program - you have two lines of code that can be expressed with one line of code. Another problem is the risk that at some parts of the program a variable is not explicitly initialized, so that its initial value may not be obvious, or even undefined, as is often the case in C++.
你经常会看到一些方法中,程序员首先声明了一些变量,然后再去进行赋值。
Mechanics
Move the declaration of the variable to where that variable is first used, just before the assignment, making sure that you haven't reduced the scope of the variable so that it isn't in scope at some other place where it is used
将变量移动到首次被使用的地方,即第一次赋值之前,并且确保你没有缩小变量的作用域而造成的一些错误。
Compile and test
编译测试
Replace the declaration and assignment with a direct initialization
以直接初始化替代声明再赋值的方式。
Compile and test
编译测试
If appropriate, declare the temp as final, and compile and test again.
如果合适的话,将变量声明final类型,编译再次测试。
Example
Start with this code.
void foo() {
int i;
// ....
i = 7;
}
The first move is to move the declaration of the variable to just before where it is assigned to:
首先将声明的变量移动到首次赋值的地方。
void foo() {
// ....
int i;
i = 7;
}
I can now compile and test this. The most common mistake is that you have reduced the scope of the variable so that it no longer is in scope at some other place where it is used. Fortunately C++ and Java compilers will give you and error if this has happened.
编译测试。通常容易出错的地方是你缩小了变量的作用域,造成一些错误。幸运的是Java和C++语言是强类型的。
Then it is time to replace the declaration + assignment with the initialization:
以初始化赋值方式取代声明和赋值方式。
void foo() {
// ....
int i = 7;
}
If this also compiles and runs as before, then it is the end of the refactoring.
编译测试,完后重构。
However if the value of i doesn't change, then it's a good idea to make that explicit by declaring i as final
如果这个变量在作用域内没有被改变过,将其改为final 类型是个很好的主意。
void foo() {
// ....
final int i = 7;
}
实际上在Eclipse中很容易使用此重构,因为Eclipse中会时时显示编译错误。
补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,