大话重构读书笔记——小步快跑的开发模式
系统重构,就是在不改变软件的外部行为的基础上,改变软件内部的结构,使其更加易于阅读、易于维护和易于变更。关于基础第一篇,请参考文章大话重构读书笔记——基础篇一。
小步快跑让我们每次重构的时候只关注一个问题,运用一个重构手法去解决这一个问题。
以下是一个非常简单的HelloWorld程序,源码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class HelloWorld 2 { 3 public string SayHelloWorld(string user) 4 { 5 DateTime date = DateTime.Now; 6 int hour = date.Hour; 7 8 string words = null; 9 if (hour >= 6 && hour < 12) 10 { 11 words = "Good morning!"; 12 } 13 else if (hour >= 12 && hour < 19) 14 { 15 words = "Good afternoon!"; 16 } 17 else 18 { 19 words = "Good night!"; 20 } 21 words = "Hi, " + user + "." + words; 22 return words; 23 } 24 }
观察程序,发现它包含三个相对独立的功能代码段,因此我们采用重构中的“抽取方法”,将它们分别抽取到三个函数getHour(), getFirstGreeting(), getSecondGreeting()中,并让原函数对其引用:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class HelloWorld 2 { 3 public string SayHelloWorld(string user) 4 { 5 int hour = getHour(); 6 return getFirstGreeting(user) + getSecondGreeting(hour); 7 } 8 9 private int getHour() 10 { 11 DateTime date = DateTime.Now; 12 return date.Hour; 13 } 14 15 private string getFirstGreeting(string user) 16 { 17 return "Hi, " + user + "."; 18 } 19 20 private string getSecondGreeting(int hour) 21 { 22 if (hour >= 6 && hour < 12) 23 { 24 return "Good morning!"; 25 } 26 else if (hour >= 12 && hour < 19) 27 { 28 return "Good afternoon!"; 29 } 30 else 31 { 32 return "Good night!"; 33 } 34 } 35 }
这样的重构虽然使程序发生了较大变化,但其中真正执行的代码却没有变化,还是那些代码。此时,如果我们还需要对用户问候语进行变更和关于时间的问候语进行变更,那么需要改动的程序依然比较大,为此,我们再次对HelloWorld程序进行分裂,运用重构中的“抽取类”,对用户问候的程序分裂到GreetingToUser类中,将关于时间的问候程序分裂到GreetingAboutTime类中。
此时, 在HelloWorld类中的代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class HelloWorld 2 { 3 public string SayHelloWorld(string user) 4 { 5 GreetingToUser greetingToUser = new GreetingToUser(user); 6 GreetingAboutTime greetingAboutTime = new GreetingAboutTime(); 7 return greetingToUser.getGreeting() + greetingAboutTime.getGreeting(); 8 } 9 }
而分裂出的GreetingToUser类代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class GreetingToUser 2 { 3 private string user; 4 public GreetingToUser(string user) 5 { 6 this.user = user; 7 } 8 public string getGreeting() 9 { 10 return "Hi, " + user + "."; 11 } 12 }
分裂出的GreetingAboutTime类代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class GreetingAboutTime 2 { 3 public int getHour() 4 { 5 DateTime date = DateTime.Now; 6 return date.Hour; 7 } 8 public string getGreeting() 9 { 10 int hour = getHour(); 11 if (hour >= 6 && hour < 12) 12 { 13 return "Good morning!"; 14 } 15 else if (hour >= 12 && hour < 19) 16 { 17 return "Good afternoon!"; 18 } 19 else 20 { 21 return "Good night!"; 22 } 23 } 24 }
此时,如果我们还需要对时间的问候语进行变更的话,仅仅需要专注于修改GreetingAboutTime就可以了,这就是因重构带来的改善。
同时,我们发现,过去只需要getHour()就好了,但是如果需要问候“元旦快乐”、“情人节快乐”的话,getHour()就没办法完成这个功能了,需要getMonth()与getDay(),继续对上述代码进行重构,将与时间有关的程序抽取到新类DateUtil中:
DateUtil类代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class DateUtil 2 { 3 private DateTime date = DateTime.Now; 4 public int getHour() 5 { 6 return date.Hour; 7 } 8 9 public int getMonth() 10 { 11 return date.Month; 12 } 13 14 public int getDay() 15 { 16 return date.Day; 17 } 18 }
与此同时,变更的GreetingAboutTime类代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class GreetingAboutTime 2 { 3 public string getGreeting() 4 { 5 DateUtil dateUtil = new DateUtil(); 6 int month = dateUtil.getMonth(); 7 int day = dateUtil.getDay(); 8 int hour = dateUtil.getHour(); 9 if (month == 1 && day == 1) 10 { 11 return "Happy New Year!"; 12 } 13 if (month ==3 && day==26) 14 { 15 return "Happy Valentine's Day"; 16 } 17 ...... 18 19 if (hour >= 6 && hour < 12) 20 { 21 return "Good morning!"; 22 } 23 if (hour >= 12 && hour < 19) 24 { 25 return "Good afternoon!"; 26 } 27 return "Good night!"; 28 } 29 }
小步快跑是一种逐步进化式的程序优化过程,它是重构思想的重要核心。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步