正如上一次所讲的那样,重构有两个基本条件,一是要保持代码在重构前后的行为基本不变,二是整个过程是受控且尽可能少地产生错误。尤其是对于第二点,产生了一系列的重构手法,每种重构手法都是一系列简单而机械的操作步骤,通过遵循这一系列的操作来实现代码的结构性调整。因此,重构的整个过程就是不断运用不同重构手法的过程,是一个相对有章可循的流程。
重构手法有大有小,大的重构手法一般由若干小的基础重构组成,进而聚沙成塔实现对代码结构大幅度的调整。完整的重构列表请参见《重构,改善既有代码的设计》一书。
例如,replace conditional with polymorphism这项复杂重构手法,就至少需要使用self encapsulate, extract method, move method, pull down method这四种基础重构手法。因此在学习类级别的复杂重构手法前,需要先掌握行级别和方法级别的基础重构手法。
重构的宏观步骤一般有如下两种:自上而下式和自下而上式。
自上而下的重构在重构前,心中已经大致知道重构后的代码将会是什么形态,然后至上而下地将步骤分解出来,并使用相应的重构步骤一一实现,最终达到重构后的形态。其流程为:
1. 识别代码中的坏味道
2. 运用设计原则,构思出修改后的目标状态
3. 将目标状态分解为一或多项重构步骤
4. 运用重构步骤
自下而上的重构则对重构后的代码没有一个完整而清晰的认识。一般而言,每种重构手法都有助于我们解决某种类型的代码坏味,而自下而上的重构则针对每个发现的代码坏味直接运用对应的重构手法,直到没有明显的坏味,此时的代码即能自动满足某种设计模式。是一种迭代的思路,也是所谓重构到模式的思路。其流程为:
1. 识别代码中的坏味道
2. 运用一项或多项重构步骤,消除坏味
3. 重复1-2,直到没有明显坏味
在一般的情况下,这两种重构流程并不是互斥的,经常交错进行或互相包含。如先运用自上而下的方法识别出代码中的坏味,然后根据设计原则重构到某个实现,再运用自下而上的方法重新寻找新的坏味,迭代重构。
由于基础重构手法比较多,而且相对比较简单。因此先列出常用的基础重构手法和简单介绍,并在最后的实践案例中结合基础重构手法来重构代码。
操作方法:
操作方法:
操作说明:
private Set channelColumns;
public String generateSql() {
String channelColumnClauseTemp = StringUtil.flat(channelColumns, ",", "", "");
String channelColumnClause;
Set columns = new TreeSet<>();
for (String str : channelColumns) {
if (ChannelId.CLT_BUS_EML_ADR.toString().equals(str)) {
columns.add(ChannelId.CLT_EML_ADR.toString());
} else {
columns.add(str);
}
}
channelColumnClause = StringUtil.flat(columns, ",", "", "");
String channelColumnsReviewTemp = "";
String channelColumnsReview = "";
if (!channelColumns.isEmpty()) {
channelColumnsReviewTemp = channelColumnClauseTemp +
(channelColumns.contains(idTypeColumn) ? "" : ("," + idTypeColumn)) + ",batch_id";
channelColumnsReview = channelColumnClause +
(channelColumns.contains(idTypeColumn) ? "" : ("," + idTypeColumn)) + ",batch_id";
} else {
channelColumnsReviewTemp = idTypeColumn + ",batch_id";
channelColumnsReview = idTypeColumn + ",batch_id";
}
StringBuffer vsql = new StringBuffer();
vsql.append("insert into ").append(Constant.DB_SCHEMA).append(".").append(tableName)
.append(" (").append(channelColumnsReview).append(")")
.append(" select distinct ").append(channelColumnsReviewTemp.replace(Constant.CITYNAME_COLUMN, "isnull(" + Constant.CITYNAME_COLUMN + ",'未知城市') as " + Constant.CITYNAME_COLUMN))
.append(" from ").append(Constant.DB_SCHEMA).append(".").append(sourceTableName).append(";\n");
return reviewTempTableSql + vsql.toString();
}
代码中的坏味有:1. 过长的方法,超过了20行或一屏,2. 变量的命名含义不清,读者无法理解channelColumnClauseTemp, channelColumnClause以及它们之间的关系,3. if-else中存在重复代码。
下来我们就来使用一系列基础重构手法来整理这段代码。
仔细观察,发现channelColumnClauseTemp变量只在if语句中使用,因此将channelColumnClauseTemp变量的申请放到if中去:
if (!channelColumns.isEmpty()) {
String channelColumnClauseTemp = StringUtil.flat(channelColumns, ",", "", "");
channelColumnsReviewTemp = channelColumnClauseTemp +
(channelColumns.contains(idTypeColumn) ? "" : ("," + idTypeColumn)) + ",batch_id";
channelColumnsReview = channelColumnClause +
(channelColumns.contains(idTypeColumn) ? "" : ("," + idTypeColumn)) + ",batch_id";
}
同样,channelColumnClause也只在if块中使用,但其中还涉及了columns及for循环部分,也一并移动到if块中:
if (!channelColumns.isEmpty()) {
String channelColumnClauseTemp = StringUtil.flat(channelColumns, ",", "", "");
channelColumnsReviewTemp = channelColumnClauseTemp +
(channelColumns.contains(idTypeColumn) ? "" : ("," + idTypeColumn)) + ",batch_id";
String channelColumnClause;
Set columns = new TreeSet<>();
for (String str : channelColumns) {
if (ChannelId.CLT_BUS_EML_ADR.toString().equals(str)) {
columns.add(ChannelId.CLT_EML_ADR.toString());
} else {
columns.add(str);
}
}
channelColumnClause = StringUtil.flat(columns, ",", "", "");
channelColumnsReview = channelColumnClause +
(channelColumns.contains(idTypeColumn) ? "" : ("," + idTypeColumn)) + ",batch_id";
}
仔细观察channelColumnsReviewTemp和channelColumnsReview两个变量的使用场景,发现它们是所拼接的sql语句的select xxx和insert (yyy)这两个部分,因此实际上是源表的列名和目标表的列名。从而将channelColumnsReviewTemp命名为sourceColumnsStr,将channelColumnsReview命名为targetColumnsStr。
同样,观察channelColumnClauseTemp和channelColumnClause,它们分别用于计算channelColumnsReviewTemp和channelColumnsReview,因此对应的命名为sourceColumnsWithoutBatchId和targetColumnsWithoutBatchId:
String sourceColumnsStr = "";
String targetColumnsStr = "";
if (!channelColumns.isEmpty()) {
String sourceColumnsWithoutBatchId = StringUtil.flat(channelColumns, ",", "", "");
sourceColumnsStr = sourceColumnsWithoutBatchId +
(channelColumns.contains(idTypeColumn) ? "" : ("," + idTypeColumn)) + ",batch_id";
String targetColumnsWithoutBatchId;
Set columns = new TreeSet<>();
for (String str : channelColumns) {
if (ChannelId.CLT_BUS_EML_ADR.toString().equals(str)) {
columns.add(ChannelId.CLT_EML_ADR.toString());
} else {
columns.add(str);
}
}
targetColumnsWithoutBatchId = StringUtil.flat(columns, ",", "", "");
targetColumnsStr = targetColumnsWithoutBatchId +
(channelColumns.contains(idTypeColumn) ? "" : ("," + idTypeColumn)) + ",batch_id";
} else {
sourceColumnsStr = idTypeColumn + ",batch_id";
targetColumnsStr = idTypeColumn + ",batch_id";
}
再观察,发现if-else代码块中同时操作了sourceColumnsStr和targetColumnsStr两个变量,不利于后续运用提取方法的重构手法。因此需要运用split block手法,将这两个变量的计算拆分到两个代码块中。先完整拷贝一份if-else代码,并在第一份中保留对sourceColumnsStr的计算,在第二份中保留对targetColumnsStr的计算,并且调整一下这两个变量申明的位置,到if-else计算逻辑的前面:
String sourceColumnsStr = "";
if (!channelColumns.isEmpty()) {
String sourceColumnsWithoutBatchId = StringUtil.flat(channelColumns, ",", "", "");
sourceColumnsStr = sourceColumnsWithoutBatchId +
(channelColumns.contains(idTypeColumn) ? "" : ("," + idTypeColumn)) + ",batch_id";
} else {
sourceColumnsStr = idTypeColumn + ",batch_id";
}
String targetColumnsStr = "";
if (!channelColumns.isEmpty()) {
String targetColumnsWithoutBatchId;
Set columns = new TreeSet<>();
for (String str : channelColumns) {
if (ChannelId.CLT_BUS_EML_ADR.toString().equals(str)) {
columns.add(ChannelId.CLT_EML_ADR.toString());
} else {
columns.add(str);
}
}
targetColumnsWithoutBatchId = StringUtil.flat(columns, ",", "", "");
targetColumnsStr = targetColumnsWithoutBatchId +
(channelColumns.contains(idTypeColumn) ? "" : ("," + idTypeColumn)) + ",batch_id";
} else {
targetColumnsStr = idTypeColumn + ",batch_id";
}
至此,可以提取两个方法:computeSourceColumnsStr()以及computeTargetColumnsStr():
private String computeTargetColumnsStr() {
String targetColumnsStr = "";
if (!channelColumns.isEmpty()) {
String targetColumnsWithoutBatchId;
Set columns = new TreeSet<>();
for (String str : channelColumns) {
if (ChannelId.CLT_BUS_EML_ADR.toString().equals(str)) {
columns.add(ChannelId.CLT_EML_ADR.toString());
} else {
columns.add(str);
}
}
targetColumnsWithoutBatchId = StringUtil.flat(columns, ",", "", "");
targetColumnsStr = targetColumnsWithoutBatchId +
(channelColumns.contains(idTypeColumn) ? "" : ("," + idTypeColumn)) + ",batch_id";
} else {
targetColumnsStr = idTypeColumn + ",batch_id";
}
return targetColumnsStr;
}
private String computeSourceColumnsStr() {
String sourceColumnsStr = "";
if (!channelColumns.isEmpty()) {
String sourceColumnsWithoutBatchId = StringUtil.flat(channelColumns, ",", "", "");
sourceColumnsStr = sourceColumnsWithoutBatchId +
(channelColumns.contains(idTypeColumn) ? "" : ("," + idTypeColumn)) + ",batch_id";
} else {
sourceColumnsStr = idTypeColumn + ",batch_id";
}
return sourceColumnsStr;
}
观察一下提取出来的两个方法,发现他们的不同之处在于computeTargetColumnsStr()中多了一个对columns集合的计算逻辑,于是将columns的计算逻辑再封装一下:
private String computeTargetColumnsStr() {
String targetColumnsStr = "";
if (!channelColumns.isEmpty()) {
Set columns = getTargetColumns();
String targetColumnsWithoutBatchId;
targetColumnsWithoutBatchId = StringUtil.flat(columns, ",", "", "");
targetColumnsStr = targetColumnsWithoutBatchId +
(channelColumns.contains(idTypeColumn) ? "" : ("," + idTypeColumn)) + ",batch_id";
} else {
targetColumnsStr = idTypeColumn + ",batch_id";
}
return targetColumnsStr;
}
private Set getTargetColumns() {
Set columns = new TreeSet<>();
for (String str : channelColumns) {
if (ChannelId.CLT_BUS_EML_ADR.toString().equals(str)) {
columns.add(ChannelId.CLT_EML_ADR.toString());
} else {
columns.add(str);
}
}
return columns;
}
经过对比,还有
String targetColumnsWithoutBatchId;
targetColumnsWithoutBatchId = StringUtil.flat(columns, ",", "", "");
这两行与String sourceColumnsWithoutBatchId = StringUtil.flat(channelColumns, ",", "", "");
存在不一致。但可以使用变量的内联重构成一样的形式。
经过整理后代码的形式如下:
public String generateSql() {
String sourceColumnsStr = computeSourceColumnsStr();
String targetColumnsStr = computeTargetColumnsStr();
StringBuffer vsql = new StringBuffer();
vsql.append("insert into ").append(Constant.DB_SCHEMA).append(".").append(tableName)
.append(" (").append(targetColumnsStr).append(")")
.append(" select distinct ").append(sourceColumnsStr.replace(Constant.CITYNAME_COLUMN, "isnull(" + Constant.CITYNAME_COLUMN + ",'未知城市') as " + Constant.CITYNAME_COLUMN))
.append(" from ").append(Constant.DB_SCHEMA).append(".").append(sourceTableName).append(";\n");
return reviewTempTableSql + vsql.toString();
}
private String computeTargetColumnsStr() {
String targetColumnsStr = "";
if (!channelColumns.isEmpty()) {
Set columns = getTargetColumns();
String targetColumnsWithoutBatchId = StringUtil.flat(columns, ",", "", "");
targetColumnsStr = targetColumnsWithoutBatchId +
(channelColumns.contains(idTypeColumn) ? "" : ("," + idTypeColumn)) + ",batch_id";
} else {
targetColumnsStr = idTypeColumn + ",batch_id";
}
return targetColumnsStr;
}
private String computeSourceColumnsStr() {
String sourceColumnsStr = "";
if (!channelColumns.isEmpty()) {
String sourceColumnsWithoutBatchId = StringUtil.flat(channelColumns, ",", "", "");
sourceColumnsStr = sourceColumnsWithoutBatchId +
(channelColumns.contains(idTypeColumn) ? "" : ("," + idTypeColumn)) + ",batch_id";
} else {
sourceColumnsStr = idTypeColumn + ",batch_id";
}
return sourceColumnsStr;
}
private Set getTargetColumns() {
Set columns = new TreeSet<>();
for (String str : channelColumns) {
if (ChannelId.CLT_BUS_EML_ADR.toString().equals(str)) {
columns.add(ChannelId.CLT_EML_ADR.toString());
} else {
columns.add(str);
}
}
return columns;
}
观察两个方法,发现只有columns和channleColumns不同,其它均相同。因此彩提炼参数的方法,为其增加参数,从而合并为一个方法。做法是先将channleColumns再重新提取一个名为columns的变量,并提到方法最开始处。再把方法中的局部变量重命名一下,就变成了:
private String computeSourceColumnsStr() {
Set columns = this.channelColumns;
String columnsStr = "";
if (!channelColumns.isEmpty()) {
String columnsStrWithoutBatchId = StringUtil.flat(columns, ",", "", "");
columnsStr = columnsStrWithoutBatchId +
(this.channelColumns.contains(idTypeColumn) ? "" : ("," + idTypeColumn)) + ",batch_id";
} else {
columnsStr = idTypeColumn + ",batch_id";
}
return columnsStr;
}
同样,将computeTargetColumnsStr()方法也处理一下:
private String computeTargetColumnsStr() {
Set columns = getTargetColumns();
String columnsStr = "";
if (!channelColumns.isEmpty()) {
String columnsStrWithoutBatchId = StringUtil.flat(columns, ",", "", "");
columnsStr = columnsStrWithoutBatchId +
(channelColumns.contains(idTypeColumn) ? "" : ("," + idTypeColumn)) + ",batch_id";
} else {
columnsStr = idTypeColumn + ",batch_id";
}
return columnsStr;
}
再将两个方法的剩下内容提取为一个新的方法,新方法含有columns作为参数:
private String computeTargetColumnsStr() {
Set columns = getTargetColumns();
return transformToString(columns);
}
private String computeSourceColumnsStr() {
Set columns = this.channelColumns;
return transformToString(columns);
}
private String transformToString(Set columns) {
String columnsStr = "";
if (!channelColumns.isEmpty()) {
String columnsStrWithoutBatchId = StringUtil.flat(columns, ",", "", "");
columnsStr = columnsStrWithoutBatchId +
(this.channelColumns.contains(idTypeColumn) ? "" : ("," + idTypeColumn)) + ",batch_id";
} else {
columnsStr = idTypeColumn + ",batch_id";
}
return columnsStr;
}
原先提取出来的两个方法就只剩下2行内容了,其中一行中变量的申明,可以将变量内联:
private String computeTargetColumnsStr() {
return transformToString(getTargetColumns());
}
private String computeSourceColumnsStr() {
return transformToString(this.channelColumns);
}
再将这两个方法内联,最终形成如下的形式:
public String generateSql() {
String sourceColumnsStr = transformToString(this.channelColumns);
String targetColumnsStr = transformToString(getTargetColumns());
StringBuffer vsql = new StringBuffer();
vsql.append("insert into ").append(Constant.DB_SCHEMA).append(".").append(tableName)
.append(" (").append(targetColumnsStr).append(")")
.append(" select distinct ").append(sourceColumnsStr.replace(Constant.CITYNAME_COLUMN, "isnull(" + Constant.CITYNAME_COLUMN + ",'未知城市') as " + Constant.CITYNAME_COLUMN))
.append(" from ").append(Constant.DB_SCHEMA).append(".").append(sourceTableName).append(";\n");
return reviewTempTableSql + vsql.toString();
}
private String transformToString(Set columns) {
String columnsStr = "";
if (!channelColumns.isEmpty()) {
String columnsStrWithoutBatchId = StringUtil.flat(columns, ",", "", "");
columnsStr = columnsStrWithoutBatchId +
(this.channelColumns.contains(idTypeColumn) ? "" : ("," + idTypeColumn)) + ",batch_id";
} else {
columnsStr = idTypeColumn + ",batch_id";
}
return columnsStr;
}
private Set getTargetColumns() {
Set columns = new TreeSet<>();
for (String str : channelColumns) {
if (ChannelId.CLT_BUS_EML_ADR.toString().equals(str)) {
columns.add(ChannelId.CLT_EML_ADR.toString());
} else {
columns.add(str);
}
}
return columns;
}
至此,整个代码中没有重复代码,每个方法长度得到控制,命名也比较恰当,重构可以至此结束。但对于高要求的风格而言,应该要求方法中的每个子方法都在同一抽象粒度上。然而transformToString()方法与后续的sql拼接并不在一个抽象粒度上,因此可以将sql拼接再提取到一个新方法中,从而增加可读性。
做法是先将vsql.toString()提取为变量:
StringBuffer vsql = new StringBuffer();
vsql.append("insert into ").append(Constant.DB_SCHEMA).append(".").append(tableName)
.append(" (").append(targetColumnsStr).append(")")
.append(" select distinct ").append(sourceColumnsStr.replace(Constant.CITYNAME_COLUMN, "isnull(" + Constant.CITYNAME_COLUMN + ",'未知城市') as " + Constant.CITYNAME_COLUMN))
.append(" from ").append(Constant.DB_SCHEMA).append(".").append(sourceTableName).append(";\n");
String insertSql = vsql.toString();
return reviewTempTableSql + insertSql;
再将return之前的部分提取到新方法generateInsertSql()中:
public String generateSql() {
String sourceColumnsStr = transformToString(this.channelColumns);
String targetColumnsStr = transformToString(getTargetColumns());
String insertSql = generateInsertSql(sourceColumnsStr, targetColumnsStr);
return reviewTempTableSql + insertSql;
}
重构过程到此结束。
整个重构过程中,使用了reorder, rename, extract variable, extract method, inline method, split for/code block, add parameter等手法。观察一下每个步骤都是可控的,如果重构在每个步骤后停止,代码依然可以运行。更重要的是,每个步骤都能被证明保持了原有代码的行为。这也是重构最重要的两个条件。
重构的案例代码:case1