代码重构技巧之重复代码 | 您所在的位置:网站首页 › 技巧8重复的数据只计算一遍怎么办 › 代码重构技巧之重复代码 |
在我们项目开发的过程中,有时写着写着顺手了,对于一些业务代码,重复编写,导致后来需要修改的时候,得整个项目到处找这些重复代码,这真是一件糟糕的事。 一、重复代码 定义重复代码并不是指的完全一模一样的代码,只要在一段代码,业务处理上,有相识之处,都能叫做重复代码。 影响重复代码太多,会造成代码过长,不易阅读,而且如果代码逻辑需要修改,会使得代码容易遗漏、不易维护。 二、重复代码案例 2.1 同一个类的多个方法间代码重复有如下代码,需要 计算水果的价格,普通水管的价格计算公式是水果数量水果价格,折扣商品计算公式是水果数量水果价格*折扣,可以看出,下面的代码,根据水果类型算价格的逻辑是重复的,如果水果价格变动,每个方法都要变动。 /** * 计算水果总价(同一个类的两个函数含有相同的表达式) * */ public class FruitsCost { public double computeMoneyWithoutPrivileges(String type, int numbers) { double prices; switch (type) { case "apple": prices = 5.5; break; case "banana": prices = 4.0; break; case "strawberry": prices = 10.5; break; default: throw new IllegalArgumentException("Illegal type : " + type); } return prices * numbers; } public double computeMoneyWithPrivileges(String type, double numbers, double discount) { double prices; switch (type) { case "apple": prices = 5.5; break; case "banana": prices = 4.0; break; case "strawberry": prices = 10.5; break; default: throw new IllegalArgumentException("Illegal type : " + type); } return prices * numbers * discount; } }解决方法 对同一个类的多个方法间代码重复,可以使用提炼函数的方式,下面主要介绍如何利用IDEA开发工具去快捷的提炼函数,拒绝手动提炼! 1:选中需要提炼的代码段 有时候,两个子类继承了一个相同的父类,子类之间存在相同的代码,我们可以考虑将相同的代码提取到父类当中,也叫函数上移。 使用IDEA上移相同的代码到父类: 1、选中需要上移的代码 有时候,重复的代码不在同一个类中,这种情况下也是也是用抽取方法的方式进行重构,但是重构后的方法,一般选中定义为静态方法。 问题代码: class MonthJudgement { public boolean judgeMonth() { Long timeStamp = System.currentTimeMillis(); // 获取当前时间戳 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String date = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp)))); String month = date.split(" ")[0].split("-")[1]; return "12".equals(month); } } class YearJudgement { public boolean judgeYear() { Long time = System.currentTimeMillis(); // 获取当前时间戳 System.out.println("获得当前时间戳"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String date = dateFormat.format(new Date(Long.parseLong(String.valueOf(time)))); return date.startsWith("2021"); } }可以分析出,两处代码中,初始化时间的逻辑是相同的,可以把这几行代码抽取出来作为静态方法,步骤如下: 1:选中YearJudgement的输出代码,使用快捷键将其移动到上方,如下动图 4、光标移到刚刚在类中创建的静态方法,再使用快捷键Ctrl+Alt+Shift+T,选择Extract Delegate |
CopyRight 2018-2019 实验室设备网 版权所有 |