java 代码细节(Introduce Explaining Variable)
来源:互联网 发布:linux 重命名文件夹 编辑:程序博客网 时间:2024/04/19 13:36
这个观点来自《重构-----改善既有代码的设计》
You have a complicated expression.
02Put the result of the expression, or parts of the expression, in a temporary variable with a name that explains the purpose.
03if ((platform.toUpperCase().indexOf("MAC") > -1) && (browser.toUpperCase().indexOf("IE") > -1) && wasInitialized() && resize > 0 ) { // do something}
04final boolean isMacOs = platform.toUpperCase().indexOf("MAC") > -1;final boolean isIEBrowser = browser.toUpperCase().indexOf("IE") > -1;final boolean wasResized = resize > 0; if (isMacOs && isIEBrowser && wasInitialized() && wasResized) { // do something}
Motivation
05Expressions can become very complex and hard to read. In such situations temporary variables can be helpful to break down the expression into something more manageable.
06Introduce Explaining Variable is particularly valuable with conditional logic in which it is useful to take each clause of a condition and explain what the condition means with a well-named temp. Another case is a long algorithm, in which each step in the computation can be explained with a temp.
07Introduce Explaining Variable is a very common refactoring, but I confess I don’t use it that much. I almost always prefer to use Extract Method if I can. A temp is useful only within the context of one method. A method is useable throughout the object and to other objects. There are times, however, when local variables make it difficult to use Extract Method. That’s when I use Introduce Explaining Variable.
Mechanics
08Declare a final temporary variable, and set it to the result of part of the complex expression.
Replace the result part of the expression with the value of the temp.
If the result part of the expression is repeated, you can replace the repeats one at a time.Compile and test.
Repeat for other parts of the expression.
Example
09I start with a simple calculation:
10double price() { // price is base price - quantity discount + shipping return _quantity * _itemPrice - Math.max(0, _quantity - 500) * _itemPrice * 0.05 + Math.min(_quantity * _itemPrice * 0.1, 100.0);}
11Simple it may be, but I can make it easier to follow. First I identify the base price as the quantity times the item price. I can turn that part of the calculation into a temp:
12double price() { // price is base price - quantity discount + shipping final double basePrice = _quantity * _itemPrice; return basePrice - Math.max(0, _quantity - 500) * _itemPrice * 0.05 + Math.min(_quantity * _itemPrice * 0.1, 100.0);}
13Quantity times item price is also used later, so I can substitute with the temp there as well:
14double price() { // price is base price - quantity discount + shipping final double basePrice = _quantity * _itemPrice; return basePrice - Math.max(0, _quantity - 500) * _itemPrice * 0.05 + Math.min(basePrice * 0.1, 100.0);}
15Next I take the quantity discount:
16double price() { // price is base price - quantity discount + shipping final double basePrice = _quantity * _itemPrice; final double quantityDiscount = Math.max(0, _quantity - 500) * _itemPrice * 0.05; return basePrice - quantityDiscount + Math.min(basePrice * 0.1, 100.0);}
17Finally, I finish with the shipping. As I do that, I can remove the comment, too, because now it doesn’t say anything the code doesn’t say:
18double price() { final double basePrice = _quantity * _itemPrice; final double quantityDiscount = Math.max(0, _quantity - 500) * _itemPrice * 0.05; final double shipping = Math.min(basePrice * 0.1, 100.0); return basePrice - quantityDiscount + shipping;}
Example with Extract Method
19For this example I usually wouldn’t have done the explaining temps; I would prefer to do that with Extract Method. I start again with
20double price() { // price is base price - quantity discount + shipping return _quantity * _itemPrice - Math.max(0, _quantity - 500) * _itemPrice * 0.05 + Math.min(_quantity * _itemPrice * 0.1, 100.0);}
21but this time I extract a method for the base price:
22double price() { // price is base price - quantity discount + shipping return basePrice() - Math.max(0, _quantity - 500) * _itemPrice * 0.05 + Math.min(basePrice() * 0.1, 100.0);} private double basePrice() { return _quantity * _itemPrice;}
23I continue one at a time. When I’m finished I get
24double price() { return basePrice() - quantityDiscount() + shipping();} private double quantityDiscount() { return Math.max(0, _quantity - 500) * _itemPrice * 0.05;} private double shipping() { return Math.min(basePrice() * 0.1, 100.0);} private double basePrice() { return _quantity * _itemPrice;}
25I prefer to use Extract Method, because now these methods are available to any other part of the object that needs them. Initially I make them private, but I can always relax that if another object needs them. I find it’s usually no more effort to use Extract Method than it is to use Introduce Explaining Variable.
26So when do I use Introduce Explaining Variable? The answer is when Extract Method is more effort. If I’m in an algorithm with a lot of local variables, I may not be able to easily use Extract Method. In this case I useIntroduce Explaining Variable to help me understand what is going on. As the logic becomes less tangled, I can always use Replace Temp with Query later. The temp also is valuable if I end up having to use Replace Method with Method Object.
- java 代码细节(Introduce Explaining Variable)
- Introduce Explaining Variable(引入解释性变量)
- Introduce Explaining Variable
- 6.5 Introduce Explaining Variable(引入解释性变量)
- 重新组织你的函数之五 :Introduce Explaining Variable(引入解释性变量)
- 重新组织函数--Introduce Explaining Variable(引入解释性变量)
- java 代码细节(split temporary variable)
- Introduce Explainning Variable
- java重构学习4:引入解释性变量(Introduce Explaing Variable)
- JAVA代码查错(细节问题)
- java 代码细节(Extract Method)
- java 代码细节(Inline Method)
- java 代码细节(Substitute Algorithm)
- Java 代码细节与优化(一)
- Java代码优化细节
- java代码优化细节
- Explaining java.lang.OutOfMemoryError: PermGen space
- Java Simple Introduce
- C#中的Action<>和Func<>
- 一个例子,理解java中的继承(来自《疯狂java讲义》)
- Boost使用入门 ---转
- 开学前倒数第十一周(6.10-6.16)
- Nginx 安装攻略手册
- java 代码细节(Introduce Explaining Variable)
- 排序算法总结
- 黑马程序员——————java中的IO
- gearman用libdrizzle实现队列持久化遇到的问题分享
- thread研究之threadlocal
- spring 集成shiro 之 自定义过滤器
- sublime 配置 gcc/g++ 环境
- 一个软件工程师在北京的反省
- Hadoop 常见问题集锦