尽快返回就是如果方法中的条件判断可以得到结果,则尽快返回该结果。

1. 检查条件,如果不满足就立即返回,不执行下面的逻辑。

2. 当包含大量的if else嵌套,代码可读性变差,也容易出现异常。

3. 在重构时, 尽量使简单判断优先执行,尽快返回,提高性能。

代码重构前

using System.Collections.Generic;
using System.Linq;
using LosTechies.DaysOfRefactoring.SampleCode.BreakMethod.After;
using Customer = LosTechies.DaysOfRefactoring.BreakResponsibilities.After.Customer; namespace LosTechies.DaysOfRefactoring.SampleCode.ReturnASAP.Before
{
public class Order
{
public Customer Customer { get; private set; } public decimal CalculateOrder(Customer customer, IEnumerable<Product> products, decimal discounts)
{
Customer = customer;
decimal orderTotal = 0m; if (products.Count() > )
{
orderTotal = products.Sum(p => p.Price);
if (discounts > )
{
orderTotal -= discounts;
}
} return orderTotal;
}
}
}

代码重构后

using System.Collections.Generic;
using System.Linq;
using LosTechies.DaysOfRefactoring.SampleCode.BreakMethod.After;
using Customer = LosTechies.DaysOfRefactoring.BreakResponsibilities.After.Customer; namespace LosTechies.DaysOfRefactoring.SampleCode.ReturnASAP.After
{
public class Order
{
public Customer Customer { get; private set; } public decimal CalculateOrder(Customer customer, IEnumerable<Product> products, decimal discounts)
{
if (products.Count() == )
return ; Customer = customer;
decimal orderTotal = products.Sum(p => p.Price); if (discounts == )
return orderTotal; orderTotal -= discounts; return orderTotal;
}
}
}

代码重构后, 可读性提高,逻辑清晰。

重构指南 - 尽快返回(Return ASAP )的更多相关文章

  1. 重构第30天 尽快返回 (Return ASAP)

    理解:把条件语句中复杂的判断用尽快返回来简化. 详解:如首先声明的是前面讲的”分解复杂判断“,简单的来说,当你的代码中有很深的嵌套条件时,花括号就会在代码中形成一个长长的箭头.我们经常在不同的代码中看 ...

  2. 重构30-Return ASAP(尽快返回)

    该话题实际上是诞生于移除箭头反模式重构之中.在移除箭头时,它被认为是重构产生的副作用.为了消除箭头,你需要尽快地return. ) { orderTotal = sum(products)) { or ...

  3. 重构指南 - 分解复杂判断(Remove Arrowhead Antipattern)

    当代码中有多层嵌套时,会降低代码的可读性,对于以后的修改也增加难度,所以我们需要分解复杂的判断并尽快返回. 重构前代码 public class Security { public ISecurity ...

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

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

  5. 重构指南 - 封装集合(Encapsulate Collection)

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

  6. oracle 调用包体的函数并返回return值

    /// <summary> /// 执行数据库包体操作,返回结果 /// </summary> /// <param name="cmdText"&g ...

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

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

  8. 重构指南 - 为布尔方法命名(Rename boolean method)

    如果一个方法中包含多个布尔类型的参数,一是方法不容易理解,二是调用时容易出错. 重构前代码 public class BankAccount { public void CreateAccount(C ...

  9. 重构指南 - 移除重复内容(Remove Duplication)

    在项目中或多或少的都存在着重复的或者功能相似的代码,如果要对代码做改动,就要修改多个地方,所以我们需要将多处重复的代码提取到一个公共的地方供统一调用,以减少代码量,提高代码可维护性. 重构前代码 pu ...

随机推荐

  1. 【bzoj4811】[Ynoi2017]由乃的OJ 树链剖分/LCT+贪心

    Description 给你一个有n个点的树,每个点的包括一个位运算opt和一个权值x,位运算有&,l,^三种,分别用1,2,3表示. 每次询问包含三个数x,y,z,初始选定一个数v.然后v依 ...

  2. 单源最短路SPFA算法

    $huaji^{233……}$模板:洛谷 P3371 #include<iostream> #include<algorithm> #include<cstdio> ...

  3. 【java】AtomicReference介绍

    本文转载自:http://www.cnblogs.com/skywang12345/p/3514623.html 概要 本章对AtomicReference引用类型的原子类进行介绍.内容包括: Ato ...

  4. MySQL事务及事务隔离级别 锁机制

    什么是事务? 当多个用户访问同一份数据时,一个用户在更改数据的过程中可能有其他用户同时发起更改请求,为保证数据库记录的更新从一个一致性状态更改为另一个一致性状态,这样的操作过程就是事务.事务具有的AC ...

  5. php 对象的自定义遍历

    php对象的自定义遍历 对手册中的案例进行分析 更好的理解foreach() 的遍历步骤 class myIterator implements Iterator { private $positio ...

  6. Bootstrap点击弹出注册登录

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Jupyter 安装与应用

    用pip安装Jupyter pip install jupyter 从命令行启动笔记本服务器 jupyter notebook 前提要先启动python,这里有一个 token值,如果不是使用默认浏览 ...

  8. yum国内镜像配置

    yum默认链接的还是国外的镜像,速度相对不理想,配置成国内的镜像会快很多,这里以阿里镜像为例进行配置: CentOS系统更换软件安装源 #base源#第一步:备份你的原镜像文件,以免出错后可以恢复.m ...

  9. QDU_CEF(补)

    C - Arthur and Table Arthur has bought a beautiful big table into his new flat. When he came home, A ...

  10. 对于position:relative,absolute,fixed的见解:

    1.switch--fixed,div脱离父元素,top,left,right,bottom都是相对于body,自己原来的位置不存在,即不占父元素位置了 2.switch--relative,div相 ...