步步为营 .NET 代码重构学习笔记 十一
步步为营 .NET 代码重构学习笔记系列
步步为营 .NET 代码重构学习笔记 二、提炼方法(Extract Method)
步步为营 .NET 代码重构学习笔记 三、内联方法(Inline Method)
步步为营 .NET 代码重构学习笔记 四、临时变量(Temporary Variable)
步步为营 .NET 代码重构学习笔记 五、分解函数和替换算法(Replace Method And Substitute Algorithm)
步步为营 .NET 代码重构学习笔记 六、移动函数和移动值域(Move Method And Move Field)
一、Remove Control Flag(移除控制标记)
动机(Motivation)
以break语句或return语句取代控制标记。
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public void CheckSecurity( string [] people) { string found = string .Empty; for ( int i = 0; i < people.Length; i++) { if (found.Equals( "" )) { if (people[i].Equals( "Don" )) { found = "Don" ; } if (people[i].Equals( "John" )) { found = "John" ; } } } SomeDataCode(found); } |
改为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public void CheckSecurity( string [] people) { string found = string .Empty; for ( int i = 0; i < people.Length; i++) { if (found.Equals( "" )) { if (people[i].Equals( "Don" )) { found = "Don" ; break ; } if (people[i].Equals( "John" )) { found = "John" ; break ; } } } SomeDataCode(found); } |
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public string FindPeople( string [] people) { string found = string .Empty; for ( int i = 0; i < people.Length; i++) { if (found.Equals( "" )) { if (people[i].Equals( "Don" )) { found = "Don" ; } if (people[i].Equals( "John" )) { found = "John" ; } } } return found; } |
改为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public string FindPeople( string [] people) { string found = string .Empty; for ( int i = 0; i < people.Length; i++) { if (found.Equals( "" )) { if (people[i].Equals( "Don" )) { return "Don" ; } if (people[i].Equals( "John" )) { return "John" ; } } } return string .Empty; } |
二、Replace Nested Conditional with Guard Clauses(以卫语句取代嵌套条件式)
动机(Motivation)
使用卫语句(guard clauses)表现所有特殊情况。
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public double GetPayAmount() { double result; if (IsDead) result = DeadAmount(); else { if (IsSeparated) result = SeparatedAmount(); else { if (IsRetired) result = RetiredPayAmount(); else result = NormalPayAmount(); } } return result; } |
改为
1 2 3 4 5 6 7 8 9 10 | public double GetPayAmount() { if (IsDead) return DeadAmount(); if (IsSeparated) return SeparatedAmount(); if (IsRetired) return RetiredPayAmount(); return NormalPayAmount(); } |
三、Introduce Null Object (引入Null对象)
动机(Motivation)
将null value(无效值)替换为null object(无效物)
示例
1 2 3 4 | if (customer == null ) plan = BillingPlan.Basic(); else plan = customer.GetPlan(); |
改为
1 2 3 4 5 6 7 | public double GetPayAmount() { if (customer.IsNull()) plan = BillingPlan.Basic(); else plan = customer.GetPlan(); } |
四、Rename Method(重新命名函数)
动机(Motivation)
修改函数名称让它人容易理解它的作用
示例
1 2 3 4 | public int Getinvcdtlmt() { return 10; } |
改为
1 2 3 4 | public int GetInvoiceAbleCreditLimit() { return 10; } |
五、Separate Query from Modifier(将查询函数和修改函数分离)
动机(Motivation)
建立两个不同的函数,其中一个负责查询,另一个负责修改。
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public string FindPeople( string [] people) { string found = string .Empty; for ( int i = 0; i < people.Length; i++) { if (found.Equals( "" )) { if (people[i].Equals( "Don" )) { SendMail(); return "Don" ; } if (people[i].Equals( "John" )) { SendMail(); return "John" ; } } } return string .Empty; } |
改为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public string FindPeople( string [] people) { string found = FindPeopleOne(people); SendMailToPeople(found); } public string FindPeopleOne( string [] people) { string found = string .Empty; for ( int i = 0; i < people.Length; i++) { if (found.Equals( "" )) { if (people[i].Equals( "Don" )) { return "Don" ; } if (people[i].Equals( "John" )) { return "John" ; } } } return string .Empty; } public void SendMailToPeople( string people) { if (! string .IsNullOrEmpty(people)) SendMail(); } |
六、Parameterize Method(令函数携带参数)
动机(Motivation)
建立单一函数,以参数表达那些不同的值
示例
1 2 3 4 5 6 7 8 9 | public double TenPercentRaise() { return salary * 1.1; } public double FivePercentRaise() { return salary * 1.05; } |
改为
1 2 3 4 | public double Raise( double factor) { return salary * factor; } |
七、Replace Parameter with Explicit Methods(以明确函数取代参数)
动机(Motivation)
针对该参数的每一个可能值,建立一个独立函数。
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | private int _height; private int _width; public void SetValue( string name, int value) { if (name.Equals( "height" )) { _height = value; return ; } if (name.Equals( "width" )) { _width = value; return ; } } |
改为
1 2 3 4 5 6 7 8 9 10 11 12 | private int _height; private int _width; public int Height { set { _height = value; } } public int Width { set { _width = value; } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架