重构第16天 封装条件(Encapsulate Conditional)
理解:本文中的“封装条件”是指条件关系比较复杂时,代码的可读性会比较差,所以这时我们应当根据条件表达式是否需要参数将条件表达式提取成可读性更好的属性或者方法,如果条件表达式不需要参数则可以提取成属性,如果条件表达式需要参数则可以提取成方法。
详解:重构前code
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ReflectorDemo
{
public class RemoteControl
{
private string[] Functions { get; set; }
private string Name { get; set; }
private int CreatedYear { get; set; } public string PerformCoolFunction(string buttonPressed)
{
// Determine if we are controlling some extra function
// that requires special conditions
if (Functions.Length > && Name == "RCA" && CreatedYear > DateTime.Now.Year - )
return "doSomething";
return string.Empty;
}
}
}
PerformCoolFunction里面的if条件判断比较复杂,看起来有点杂乱,所以就把它提出来。
重构后代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ReflectorDemo
{
public class RemoteControl
{
private string[] Functions { get; set; }
private string Name { get; set; }
private int CreatedYear { get; set; } private bool HasExtraFunctions
{
get { return Functions.Length > && Name == "RCA" && CreatedYear > DateTime.Now.Year - ; }
} public string PerformCoolFunction(string buttonPressed)
{
// Determine if we are controlling some extra function
// that requires special conditions
if (HasExtraFunctions)
return "doSomething";
return string.Empty;
}
}
}
我们把条件表达式封装成HasExtraFunctions属性,这样先前的条件判断就成了if (HasExtraFunctions) ,所以这样就在很大程度上提高了可读性。
这个重构在很大程度上能改善代码的可读性,尤其是在一个逻辑很复杂的应用中,把这些条件判断封装成一个有意义的名字,这样很复杂的逻辑也会立刻变得简单起来。
重构第16天 封装条件(Encapsulate Conditional)的更多相关文章
- 重构指南 - 使用多态代替条件判断(Replace conditional with Polymorphism)
多态(polymorphism)是面向对象的重要特性,简单可理解为:一个接口,多种实现. 当你的代码中存在通过不同的类型执行不同的操作,包含大量if else或者switch语句时,就可以考虑进行重构 ...
- 【Java重构系列】重构31式之封装集合
2009年,Sean Chambers在其博客中发表了31 Days of Refactoring: Useful refactoring techniques you have to know系列文 ...
- CSharpGL(30)用条件渲染(Conditional Rendering)来提升OpenGL的渲染效率
CSharpGL(30)用条件渲染(Conditional Rendering)来提升OpenGL的渲染效率 当场景中有比较复杂的模型时,条件渲染能够加速对复杂模型的渲染. 条件渲染(Conditio ...
- 重构改善既有代码设计--重构手法16:Introduce Foreign Method (引入外加函数)&& 重构手法17:Introduce Local Extension (引入本地扩展)
重构手法16:Introduce Foreign Method (引入外加函数)你需要为提供服务的类增加一个函数,但你无法修改这个类.在客户类中建立一个函数,并以第一参数形式传入一个服务类实例. 动机 ...
- [读书笔记] 二、条件注解@Conditional,组合注解,元注解
一.条件注解@Conditional,组合注解,元注解 1. @Conditional:满足特定条件创建一个Bean,SpringBoot就是利用这个特性进行自动配置的. 例子: 首先,两个Condi ...
- Spring Boot实战笔记(八)-- Spring高级话题(条件注解@Conditional)
一.条件注解@Conditional 在之前的学习中,通过活动的profile,我们可以获得不同的Bean.Spring4提供了一个更通用的基于条件的Bean的创建,即使用@Conditional注解 ...
- 条件随机场Conditional Random Field-CRF入门级理解
条件随机场Conditional Random Field-CRF入门级理解 有向图与无向图模型 CRF模型是一个无向概率图模型,更宽泛地说,它是一个概率图模型.现实世界的一些问题可以用概率图模型 ...
- 一次项目代码重构-使用spring容器干掉条件判断
一次项目代码重构-使用spring容器干掉条件判断 这是在一次公司项目中进行重构时,一些复杂业务时想到的一个去掉一些if else的办法.能够使代码逻辑更加清晰,减少一些业务上的耦合. 业务说明 我所 ...
- 18.AutoMapper 之条件映射(Conditional Mapping)
https://www.jianshu.com/p/8ed758ed3c63 条件映射(Conditional Mapping) AutoMapper 允许你给属性添加条件,只有在条件成立的情况下该成 ...
随机推荐
- Scala 深入浅出实战经典 第67讲:Scala并发编程匿名Actor、消息传递、偏函数解析
王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...
- 我的常用mixin 之 px
$unit: .667px; @function px($value){ @return $unit * $value; } $unit: 1px; 时 px(1) 就是 1px 一般情况下,设计师给 ...
- 解决“com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536”问题(l转)
同时在工程中引入了多个第三方jar包,导致调用的方法数超过了android设定的65536个(DEX 64K problem),进而导致dex无法生成,也就无法生成APK文件. 解决办法如下: 1.谷 ...
- Swift基础--手势识别(双击、捏、旋转、拖动、划动、长按)
// // ViewController.swift // JieUITapGestureRecognizer // // Created by jiezhang on 14-10-4. // ...
- .Net事件管道详解图
- 桌面oracle 11g导入多年库的dump备忘
接到客户6G的dump文件.先导入桌面orcale ,imp提示出错,执行impdp后如下 连接到: Oracle Database 11g Express Edition Release 11.2. ...
- 为Eclipse添加Java和Android SDK源代码
1.添加jdk源码进入eclipse Ctrl + Click -->Attached Source 路径:D:\Program Files\Java\jdk1.8.0_45\src.zip 2 ...
- 调用 google speech api (使用Google语音识别引擎)
完全参考自: http://mikepultz.com/2011/03/accessing-google-speech-api-chrome-11/ http://aiku.me/bar/104480 ...
- CSS 会被继承的属性
文本 color(颜色,a元素除外) direction(方向) font(字体) font-family(字体系列) font-size(字体大小) font-style(用于设置斜体) font- ...
- Apache2.4中开通HTTP基本认证
Apache2.4中开通HTTP基本认证,需要在Apache的配置文件中增加如下代码 WSGIPassAuthorization On 否则则无法认证