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

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. 洛谷P3711 仓鼠的数学题(伯努利数+多项式求逆)

    题面 传送门 题解 如果您不知道伯努利数是什么可以去看看这篇文章 首先我们把自然数幂和化成伯努利数的形式 \[\sum_{i=1}^{n-1}i^k={1\over k+1}\sum_{i=0}^k{ ...

  2. 使用C语言封装数组,动态实现增删改查

    myArray.h : #pragma once //包含的时候只包含一次 #include <stdio.h> #include <stdlib.h> #include &l ...

  3. ubtuntu 如何查看内存用量 mongostat详解

    free -h top free或者top或者cat /proc/meminfo mongostat是mongdb自带的状态检测工具,在命令行下使用.它会间隔固定时间获取mongodb的当前运行状态, ...

  4. PowerShell 如何 远程连接【转】

    转自: 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://bobzy.blog.51cto.com/2109336/1181249 ...

  5. Gym - 101908G 二分答案+最大流

    After the end of the truck drivers' strike, you and the rest of Nlogônia logistics specialists now h ...

  6. centos6+如何对外开放80,3306端口号或者其他端口号

    1.查看防火墙对外开放了哪些端口 [root@hadoop110 ~]# iptables -L -n Chain INPUT (policy ACCEPT) target prot opt sour ...

  7. flex 实例Demo

    Flex 页面布局 很方便 快捷 <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  8. gevent 传参, 中文编码

    #coding=utf-8 import os from gevent import monkey,pool monkey.patch_all() import gevent import time ...

  9. flask之werkzeug

    密码存储的主要形式: 明文存储:肉眼就可以识别,没有任何安全性. 加密存储:通过一定的变换形式,使得密码原文不易被识别. 密码加密的几类方式: 明文转码加密:BASE64, 7BIT等,这种方式只是个 ...

  10. FTP 两种连接模式

    简介 FTP协议要用到两个TCP连接, 一个是命令连接,用来在FTP客户端与服务器之间传递命令:另一个是数据连接,用来上传或下载数据.通常21端口是命令端口,20端口是数据端口.当混入主动/被动模式的 ...