当代码中充斥着若干条件判断时,代码的真正意图会迷失于这些条件判断之中。这时我喜欢将条件判断提取到一个易于读取的属性或方法(如果有参数)中。重构之前的代码如下:

public class RemoteControl {
private String[] Functions;//getter setter
private String Name;//getter setter
private int CreatedYear;//getter setter public String PerformCoolFunction(String buttonPressed) {
// Determine if we are controlling some extra function
// that requires special conditions
if (Functions.length > 1 && Name == "RCA" && CreatedYear > new Date().getYear() - ) {
return "doSomething";
}
return null;
}
}
重构之后,代码的可读性更强,意图更明显:
public class RemoteControl {
private String[] Functions;//getter setter
private String Name;//getter setter
private int CreatedYear;//getter setter
private Boolean HasExtraFunctions; public Boolean getHasExtraFunctions() {
return Functions.length > 1 && Name == "RCA" && CreatedYear > new Date().getYear() - ;
}
public String PerformCoolFunction(String buttonPressed) {
// Determine if we are controlling some extra function
// that requires special conditions
if (HasExtraFunctions) {
return "doSomething";
}
return null;
}
}

重构16-Encapsulate Conditional(封装条件)的更多相关文章

  1. 【Java重构系列】重构31式之封装集合

    2009年,Sean Chambers在其博客中发表了31 Days of Refactoring: Useful refactoring techniques you have to know系列文 ...

  2. 重构第16天 封装条件(Encapsulate Conditional)

    理解:本文中的“封装条件”是指条件关系比较复杂时,代码的可读性会比较差,所以这时我们应当根据条件表达式是否需要参数将条件表达式提取成可读性更好的属性或者方法,如果条件表达式不需要参数则可以提取成属性, ...

  3. 重构指南 - 使用多态代替条件判断(Replace conditional with Polymorphism)

    多态(polymorphism)是面向对象的重要特性,简单可理解为:一个接口,多种实现. 当你的代码中存在通过不同的类型执行不同的操作,包含大量if else或者switch语句时,就可以考虑进行重构 ...

  4. 一次项目代码重构-使用spring容器干掉条件判断

    一次项目代码重构-使用spring容器干掉条件判断 这是在一次公司项目中进行重构时,一些复杂业务时想到的一个去掉一些if else的办法.能够使代码逻辑更加清晰,减少一些业务上的耦合. 业务说明 我所 ...

  5. 重构指南 - 封装条件(Encapsulate Conditional)

    封装就是将相关的方法或者属性抽象成为一个对象. 封装的意义: 对外隐藏内部实现,接口不变,内部实现自由修改. 只返回需要的数据和方法. 提供一种方式防止数据被修改. 更好的代码复用.   当代码中包含 ...

  6. 重构第18天 用条件语句来代替异常(Replace exception with conditional)

    理解:本文中的“使用条件判断代替异常”是指把没有必要使用异常做判断的条件尽量改为条件判断. 详解: 重构前代码: public class Microwave { private IMicrowave ...

  7. 重构18-Replace exception with conditional(条件替代异常)

    重构没有什么出处,是我平时经常使用而总结出来的.欢迎您发表任何改进意见或建议.我相信一定还有其他比较好的重构可以解决类似的问题.  我曾无数次面对的一个代码坏味道就是,使用异常来控制程序流程.您可能会 ...

  8. 重构:用Command替换条件调度程序

    注:该随笔受启发于 <重构与模式>  第七章 第7.6小节 用Command替换条件调度程序 . 对于Command不做过多解释,这里我找了两个例子.供部分园友参阅:Command例子1  ...

  9. windows环境下封装条件wait和signal

    linux 环境有提供好的pthread_cond_wait() 和 phread_signal().pthread_broadcast() windows需要自己封装,利用semophore控制线程 ...

随机推荐

  1. php连接oracle10数据库 转载

    本文转载自:http://blog.csdn.net/wzg199088/article/details/6678241 一.配置环境: 访问Oracle8以上的数据库需要用到Oracle8Call- ...

  2. 利用一些码农Trick去搞一搞G和T的单词

    根据自然语言处理中的Zipf统计定律,在自然语言的语料库里,一个单词出现的频率与它在频率表里的排名成反比.因此,我们有理由认为,可以根据这个频率表进行一下排序,以及purning.由于精力有限,没有足 ...

  3. tableviewcell 系统工具删除:左滑删除,选中多个删除

    在编辑tableview的时候,我们有时候会需要编辑对一些cell进行删除(增加),在这个时候,我们最好用系统的方法来进行增删的操作 // // text1Controller.m // text / ...

  4. (剑指Offer)面试题18:树的子结构

    题目: 输入两棵二叉树A和B,判断B是不是A的子结构. 二叉树结构定义如下: struct TreeNode{ int val; TreeNode* left; TreeNode* right; }; ...

  5. 用javascript实现简体和繁体字间的转换

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  6. Pre-compile (pre-JIT) your assembly on the fly, or trigger JIT compilation ahead-of-time (转)

    Introduction All .NET developers know that one of the best features of the CLR is JIT-compilation: J ...

  7. EntityFramework Code First 优化-IIS 8的第一次优化请求配置

    首先需要在Window中添加Application Initialization Application Initialization 在IIS中配置Application Pool 编辑Applic ...

  8. SGU 538. Emoticons 水题

    538. Emoticons 题目连接: http://acm.sgu.ru/problem.php?contest=0&problem=538 Description A berland n ...

  9. Codeforces Round #336 (Div. 2)B. Hamming Distance Sum 前缀和

    B. Hamming Distance Sum 题目连接: http://www.codeforces.com/contest/608/problem/A Description Genos need ...

  10. [ES6] 14. Generator -- 1. yield & next()

    Generators in ECMAscript 6 are first-class coroutines that produce encapsulated suspended execution  ...