Reverse Conditional -- 反转条件

Reverse Conditional

Refactoring contributed by Bill Murphy and Martin Fowler

You 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。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值