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

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. 题解 P2613 【【模板】有理数取余】

    题目链接 我们先看这个式子: $c=\dfrac{a}{b}$ $ $ $ $ $mod$ $ $ $ $ $19260817$ 某正常高中生:这$……$ --- 对于这个 $c$ . 显然,它很可能 ...

  2. 190308python-MySQL

    一.Python连接MySQL import pymysql conn = pymysql.connect(host='192.168.100.4', port=3306, user='dongfei ...

  3. APUE第八章-进程控制

    一.进程标识 二.函数fork 1.写时复制,copy-on-write 2.文件共享,父进程等待子进程完成,子进程结束后,它对任一共享描述符的读写操作的文件偏移量已做相应的更新,同时操作时,可以考虑 ...

  4. 【转】xml文件中加入本地的dtd约束文件

    首先,我是以加载Struts2的来演示: 1 我们可以看到,越是文件中的 显示的是PUBLIC, 即从网络中获取约束文件dtd ,此时我需要将其配置成从自己的本地来获取dit文件 首先,先要有stru ...

  5. 扩增子分析QIIME2. 1简介和安装

    原网站:https://blog.csdn.net/woodcorpse/article/details/75103929 声明:本文为QIIME2官方帮助文档的中文版,由中科院遗传发育所刘永鑫博士翻 ...

  6. 洛谷 P4280 bzoj1786 [AHOI2008]逆序对(dp)

    题面 luogu bzoj 题目大意: 给你一个长度为\(n\)的序列,元素都在\(1-k\)之间,有些是\(-1\),让你把\(-1\)也变成\(1-k\)之间的数,使得逆序对最多,求逆序对最少是多 ...

  7. c#工具类之Bitmap缩放帮忙类

    using System.Drawing; using System.Drawing.Drawing2D; /// <summary> /// BitmapHelper /// </ ...

  8. Win10如何新建用户怎么添加新账户

    https://jingyan.baidu.com/article/25648fc162d5899190fd0069.html 很多朋友都是安装完Windows10系统后,直接使用超级管理员账号登录系 ...

  9. vue项目构建过程

    # template 模版项目 > A Vue.js project* 构建过程* 安装过程* 差异点* 打包优化 ## 构建过程```bashbogon:vue-cli caoke$ vue ...

  10. python基础学习-思维导图总结