Reverse Conditional -- 反转条件
2013-03-18 20:47
183 查看
Reverse Conditional
Refactoring contributed by Bill Murphy and Martin FowlerYou have a conditional that would be easier to understand if you reversed its sense.
通过反转条件语句能使得条件语句更容易理解。
Reverse the sense of the conditional and reorder the conditional's clauses.
反转条件并且重新排列条件分支。
if ( !isSummer( date ) ) charge = winterCharge( quantity ); else charge = summerCharge( quantity );
if ( isSummer( date ) ) charge = summerCharge( quantity ); else charge = winterCharge( quantity );
Motivation
Often conditionals can be phrased in way that makes them hard to read. This is particularly the case when they have a not in front of them. If the conditional only has a "then" clause and no "else" clause, this is reasonable.However if the conditional has both clauses, then you might as well reverse the conditional.
经常有一些条件语句表达方式不同于日常的表达方式,特别是他们还不是显而易见的时候。如果条件语句只有if没有else,还算是可以的。如果条件语句具有完整的分支时,你应该使用反转条件重构手段使得符合日常的表达方式。
Mechanics
Remove negative from conditional.移除否定条件
Switch clauses
交换条件分支。
Compile and test.
编译测试
There's further discussion on this kind of refactoring on the wiki.
更多内容请查看wiki。
相关文章推荐
- 反转单链表 和 将数组按照某种条件分为前后两个部分
- LeetCode-92-Reverse Linked List II 链表反转 各种边界条件
- 查看Oracle当前用户下的信息(用户,表视图,索引,表空间,同义词,存储过程函数,约束条件)
- ibatis按照or查询条件进行查询
- Rust进阶之条件编译
- 控制反转 (IOC) 设计模式
- thinkphp根据条件取得id值并更新个别字段的值
- 1943:满足条件的整数 C
- 如何界定出手机重力感应触发条件?
- 转载-SQL中的where条件,在数据库中提取与应用浅析
- (转)找符合条件的整数
- 链表反转C实现(递归与循环)
- JS中的条件语句/循环语句/跳转语句
- 条件判断
- 聚集索引非聚集索引使用条件
- ISA虚拟化的条件
- 注意区分left join on 后面的条件 和where 后面的条件
- 实现函数 functionFunction,调用之后满足如下条件: 1、返回值为一个函数 f 2、调用返回的函数 f,返回值为按照调用顺序的参数拼接,拼接字符为英文逗号加一个空格,即 ', ' 3、所
- 实时操作系统和优先级反转
- Axure PR 全局变量、页面传值与条件判断