五、重构2:对GetFrequentRenterPoints方法的参数进行重构
重构最重要的思想就是普通程序也能写出优秀的程序。重构一个项目的巨大工作量就是修改变量名、提取方法、抽取接口、修改方法参数等简单的工作。作为一个普通的程序就可以通过这些简单且容易完成的工作目标来提升自己的编码能力,加深自己对项目的认知,从而为最高层次的重构打下基础。在这个过程中发现Bug、修改Bug,这不是重构。优化不是重构。强化异常捕捉、增加预防性代码不是重构。让代码更容易测试不是重构——尽管重构能达到相同的效果。这些所有的事都是有益的。但这些都不是重构。
我们观察文章中的共享充电宝计费代码中,发现Customer类的GetFrequentRenterPoints()方法也需要进行重构,GetFrequentRenterPoints方法有三个参数,这三个参数不是所有参数都是必须的,本文使用重构中的“删除参数”,对这个方法的参数进行重构。
1.在Visual Studio 2019代码编辑器中打开Customer.cs文件,找到GetFrequentRenterPoints方法并选中,让其突出显示。如下图。
2. 接下来,执行以下操作之一:
在Visual Studio 2019的菜单栏上选择“编辑 > 重构 > 删除参数” 。
在Visual Studio 2019的菜单栏上选择“编辑 > 重构 > 重新排列参数” 。
3. 在弹出的“更改签名”对话框中,可以使用右侧的按钮更改方法的参数数量与位置。然后点击“确定”按钮。如下图。
4.在进行了方法参数重构之后,GetFrequentRenterPoints方法代码如下:
public int GetFrequentRenterPoints(Rental item, decimal amount) { int frequentRenterPoints = 0; //计算积分 if (item.Power.PriceCode == PowerBank.HighTraffic && item.RentedTime > 4) { frequentRenterPoints += (int)Math.Ceiling(amount * 1.5M); } else frequentRenterPoints += (int)Math.Ceiling(amount); return frequentRenterPoints; }
5. 在Visual Studio 2019中打开测试项目LeasePowerBankTest中的UnitTest1.cs测试类文件,修改测试类中的测试方法ValidGetFrequentRenterPointsTest,代码如下:
[TestMethod] public void ValidGetFrequentRenterPointsTest() { int expected = 5; //创建用户 var customer = new Customer("张三"); //创建充电宝 PowerBank regularPowerBank = new PowerBank("低-充电宝", PowerBank.LowTraffic); //创建租赁数据 var rental1 = new Rental(regularPowerBank, 5); // Act int actual = customer.GetFrequentRenterPoints(rental1,5); // Assert Assert.AreEqual(expected, actual, 0.001, "积分计算错误"); }
6. 在Visual Studio 2019的菜单栏上找到“测试-->运行所有测试”菜单项。或者在“测试资源管理器中”选择 “在视图中运行所有测试”按钮, 以运行测试。测试全部通过。如下图。
7.在经过上面的重构之后,GetFrequentRenterPoints方法的参数减少到了两个。现在再仔细品品这个方法的参数,发现方法中的amount参数也不是必须的,也是可以减少的。继续进行重构。
8.在Visual Studio 2019代码编辑器中打开Customer.cs文件,选中GetFrequentRenterPoints方法,让其突出显示,然后单击鼠标右键,在弹出的快捷菜单中选择“快速操作和重构”,会显示如图中方框中的菜单,选择“更改签名”。如下图。
9. 在弹出的“更改签名”对话框中,可以使用右侧的按钮更改方法中的参数数量与位置。然后点击“确定”按钮。如下图。
10.经过上面的重构步骤之后,GetFrequentRenterPoints方法的代码如下:
public int GetFrequentRenterPoints(Rental item) { int frequentRenterPoints = 0; decimal amount = GetAmount(item); //计算积分 if (item.Power.PriceCode == PowerBank.HighTraffic && item.RentedTime > 4) { frequentRenterPoints += (int)Math.Ceiling(amount * 1.5M); } else frequentRenterPoints += (int)Math.Ceiling(amount); return frequentRenterPoints; }
11.在Visual Studio 2019中打开测试项目LeasePowerBankTest中的UnitTest1.cs测试类文件,修改测试类中的测试方法ValidGetFrequentRenterPointsTest,代码如下:
[TestMethod] public void ValidGetFrequentRenterPointsTest() { int expected = 5; //创建用户 var customer = new Customer("张三"); //创建充电宝 PowerBank regularPowerBank = new PowerBank("低-充电宝", PowerBank.LowTraffic); //创建租赁数据 var rental1 = new Rental(regularPowerBank, 5); // Act int actual = customer.GetFrequentRenterPoints(rental1); // Assert Assert.AreEqual(expected, actual, 0.001, "积分计算错误"); }
12. 在Visual Studio 2019的菜单栏上找到“测试-->运行所有测试”菜单项。或者在“测试资源管理器中”选择 “在视图中运行所有测试”按钮, 以运行测试。测试全部通过。
https://www.meipian.cn/3q4ggu3d?share_depth=1
https://blog.51cto.com/weixincrm/3190833
https://www.meipian.cn/3q7q67zd?share_depth=1
https://www.acfun.cn/a/ac30419661
https://itbbs.pconline.com.cn/soft/54651509.html
https://zhuanlan.zhihu.com/p/393956118
https://blog.51cto.com/weixincrm/3211030
https://xueqiu.com/4824316523/192398602
https://tieba.baidu.com/p/7468257539
https://www.cnblogs.com/myshowtime/p/15071212.html
https://blog.youkuaiyun.com/lesive_1421/article/details/119186075
https://blog.youkuaiyun.com/lesive_1421/article/details/119186117
https://blog.youkuaiyun.com/lesive_1421/article/details/119186172
https://blog.youkuaiyun.com/lesive_1421/article/details/119186257
https://blog.youkuaiyun.com/lesive_1421/article/details/119186329
https://zhinan.sogou.com/guide/d316514420851.htm
https://zhinan.sogou.com/guide/d316514420847.htm
https://zhinan.sogou.com/guide/d316514420844.htm
https://zhinan.sogou.com/guide/d316514420844.htm
https://zhinan.sogou.com/guide/d316514420690.htm
https://www.meipian.cn/3o5qtuvg?share_depth=1
https://www.im286.net/thread-24265831-1.html
https://zhuanlan.zhihu.com/p/383220082
https://xueqiu.com/4824316523/186894941
https://blog.youkuaiyun.com/yunkeyi/article/details/118151701
https://blog.51cto.com/weixincrm/2941363
https://itbbs.pconline.com.cn/soft/54641756.html
https://www.acfun.cn/a/ac29939481
http://www.gzyltx.com/Articles/ArticleInfo?articleID=122&&menuID=80
https://720yun.com/u/3e0jkpmyvn9
https://www.processon.com/u/60d2ecd47d9c087f546def66/profile
http://www.kuaijidao.cn/user/43802
https://tieba.baidu.com/p/7420124007
https://weibo.com/ttarticle/p/show?id=2309404651654816333999
https://www.meipian.cn/3o8xy0mq?share_depth=1
https://zhuanlan.zhihu.com/p/383613859
https://www.jianshu.com/p/eccd17f78fdc
https://blog.youkuaiyun.com/yunkeyi/article/details/118190886
https://xueqiu.com/4824316523/187087143
https://www.im286.net/thread-24266077-1.html
https://blog.51cto.com/weixincrm/2945582
https://itbbs.pconline.com.cn/soft/54642138.html
https://www.acfun.cn/a/ac29979759
如下图。