Replace Assignment with Initialization -- 以初始化值取代赋值
来源:互联网 发布:监狱建筑师 mac 汉化 编辑:程序博客网 时间:2024/05/25 23:29
Replace Assignment with Initialization
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中会时时显示编译错误。
- Replace Assignment with Initialization -- 以初始化值取代赋值
- 赋值 vs 初始化(Assignment vs Initialization)
- [笔记]Effective C++12条(以initialization取代assignment动作)
- Replace Data Value with Object(以对象取代数据值)
- Item 12. 赋值和初始化(Assignment and Initialization Are Different)
- 读书笔记: effective C++ 条款12 尽量以initialization 动作取代assignment
- 8.2 replace data value with object(以对象取代数据值)
- 重新组织数据之二 :Replace Data Value with Object(以对象取代数据值)
- Replace Temp With Query(以查询取代临时变量)
- Replace Method with Method Object(以函数对象取代函数)
- Replace Array with Object(以对象取代数组)
- Replace Record with Data Class(以数据类取代记录 )
- Replace Type Code with Class(以类取代型别码)
- Replace Type Code with Subclasses(以子类取代型别码)
- Replace Subclass with Fields(以值域取代子类)
- Replace Conditional with Polymorphism(以多态取代条件式)
- 10.8 replace parameter with methods(以函数取代参数)
- 8.5 replace array with object (以对象取代数组)
- 一对多与多对一关联关系
- Reduce Scope of Variable -- 缩小变量作用域
- 这段代码的第17行为什么会出现空指针异常呢
- 一对一关联
- 多对多关联
- Replace Assignment with Initialization -- 以初始化值取代赋值
- hdu 1242 优先队列广搜
- windows 下架设svn服务器
- 组件关联关系
- 【数据结构】并查集之一
- 实现一个命令行linux终端应用程序,把输入的指定文件,映射到内存中,并打印出文件内容
- 采用xml格式的传输,两种实现方式
- c语言指针例题
- inverse属性