惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
Blog — PlanetScale
Blog — PlanetScale
阮一峰的网络日志
阮一峰的网络日志
M
MIT News - Artificial intelligence
月光博客
月光博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
U
Unit 42
博客园_首页
WordPress大学
WordPress大学
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
F
Fortinet All Blogs
腾讯CDC
罗磊的独立博客
IT之家
IT之家
I
InfoQ
V
V2EX
博客园 - 叶小钗
A
About on SuperTechFans
Y
Y Combinator Blog
C
Check Point Blog
量子位
Martin Fowler
Martin Fowler
Vercel News
Vercel News

博客园 - 网络金领

C# WinForm 导出导入Excel/Doc 完整实例教程[使用Aspose.Cells.dll] DataTable的数据批量写入数据库 C# .NET锁屏程序(顺带屏蔽任务管理器) 步步为营 .NET 代码重构学习 十一 步步为营 .NET 代码重构学习笔记 十 步步为营 .NET 代码重构学习笔记 九 步步为营 .NET 代码重构学习笔记 八 步步为营 .NET 代码重构学习笔记 七 步步为营 .NET 代码重构学习笔记 六、移动函数和移动值域(Move Method And Move Field) 步步为营 .NET 代码重构学习笔记 四、临时变量(Temporary Variable) 步步为营 .NET 代码重构学习笔记 二、提炼方法(Extract Method) 步步为营 .NET 代码重构学习笔记 三、内联方法(Inline Method) 步步为营 .NET 代码重构学习笔记 一、为何要代码重构 步步为营 .NET 设计模式学习笔记 二十四、Factory Method(工厂方法模式) 步步为营 .NET 设计模式学习笔记 二十三、Interpreter(解释器模式) 步步为营 .NET 设计模式学习笔记 二十二、Memento(备望录模式) 步步为营 .NET 设计模式学习笔记 二十一、Visitor(访问者模式) 步步为营 .NET 设计模式学习笔记 二十、Mediator(中介者模式) 步步为营 .NET 设计模式学习笔记 十九、Chain of Responsibility(职责链模式)
步步为营 .NET 代码重构学习笔记 五、分解函数和替换算法(Rep...
网络金领 · 2011-06-02 · via 博客园 - 网络金领

Replace Method with Method Object

概述

将这个函数放进一个单独对象中,如此一来局部变量就成了对象内的值域(field),然后你可以在同一个对象中将这个大型函数分解为数个小型函数.

动机(Motivation)

小型函数优美动人,只要将相对独立的代码从大型函数中提炼出来,就可以大在提高代码的可读性.

示例

1 public int Gamma(int inputValue, int quantity, int yearToDate)
3     int importantValue1 = inputValue * quantity + DateTime.Now.Minute;
4     int importantValue2 = inputValue * yearToDate + 100;
5     if ((yearToDate - importantValue1) > 100)
6         importantValue2 -= 20;
7     int importantValue3 = importantValue2 * 7;
8     return importantValue3 - 2 * importantValue1;

改为

01 private int importantValue1;
02 private int importantValue2;
03 private int importantValue3;
05 public int Gamma(int inputValue, int quantity, int yearToDate)
07     importantValue1 = inputValue * quantity + DateTime.Now.Minute;
08     importantValue2 = inputValue * yearToDate + 100;
09     ImportantThing(yearToDate);
10     importantValue3 = importantValue2 * 7;
11     return importantValue3 - 2 * importantValue1;
14 private void ImportantThing(int yearToDate)
16     if ((yearToDate - importantValue1) > 100)
17         importantValue2 -= 20;

Substitute Algorithm(替换你的算法)

概述

将函数本体(method body)替换为另一个算法。

动机(Motivation)

如果你发现做一件事可以有更清晰的方式,就应该以较清晰的方式取代复杂方式。可以把一些复杂的东西分解为较简单的小块,但有时你就是必须壮士断腕,删掉整个算法,代之较简单的算法。

示例

01 public string FoundPerson(string[] people)
03     for (int i = 0; i < people.Length; i++)
05         if (people[i].Equals("Don"))
07             return "Don";
09         if (people[i].Equals("John"))
11             return "John";
13         if (people[i].Equals("Kent"))
15             return "Kent";

改为

01 public string FoundPerson(string[] people)
03     List<string> candidates = new List<string>() { "Don", "John", "Kent" };
04     for (int i = 0; i < people.Length; i++)
06         if (candidates.Contains(people[i]))
07             return people[i];

总结

小型函数优美动人,用较清晰方式取代复杂方式,易于阅读,