早上看了这样的一坨代码(一些业务信息我抹去了,不用太在意具体上面这些filter的具体行为,firstname和lastname也是替代了原始的字段)
可以作为一个很好的重构实例。我其实并不知道这段代码要干什么,但是即便如此,我们也可以重构代码
第一眼完全看不懂在写什么
private static final String FILTER_OR = "firstName='%s' or lastName='%s'";
private static final String FILTER_AND = "firstName='%s' and lastName='%s'";
private static final String FILTER_OR_CON = "firstName='%s' or lastName='%s' con";
private static final String FILTER_AND_CON = "firstName='%s' and lastName='%s' con";
private static final String FILTER_USER_ID_NAME = "firstName='%s' and lastName='%s' and userId";
private String filterBuilder_original(List<List<String>> searchcriterias) {
StringBuilder filters = new StringBuilder();
for (int i = 0; i < searchcriterias.size(); i++) {
// 第一行如果要特殊处理,那就单独拿出来
// 双层if的逻辑是肯定可以改写的
if (i == 0) {
//searchcriterias.get(i)代码是重复代码,而且get出来是什么东西,不知道
// searchcriterias.get(i).get(0)这个不知道是什么的东西get出来的这个东西是什么东西?也不知道
// 至于0和1完全就是一个魔法值,不知道具体的含义,第一个index是什么,第二个是什么?是只有这两个index吗?
if (searchcriterias.get(i).get(0).equals(searchcriterias.get(i).get(1))) {
filters.append(
String.format(
FILTER_OR, searchcriterias.get(i).get(0), searchcriterias.get(i).get(1)));
} else {
// String命名为a。。。
String a =
String.format(
FILTER_AND, searchcriterias.get(i).get(0), searchcriterias.get(i).get(1));
filters.append(a);
}
} else {
if (searchcriterias.get(i).get(0).equals(searchcriterias.get(i).get(1))) {
filters.append(
String.format(
FILTER_OR_CON, searchcriterias.get(i).get(0), searchcriterias.get(i).get(1)));
} else {
filters.append(
String.format(
FILTER_AND_CON, searchcriterias.get(i).get(0), searchcriterias.get(i).get(1)));
}
}
}
// 这行用的信息和第一行一样
// 需要指出的是这段代码没有考虑到链表为空的情况,是否在上层有判断,我无法得知,但就这段代码而言,如果list为null会空指针错误
filters.append(
String.format(
FILTER_USER_ID_NAME, searchcriterias.get(0).get(0), searchcriterias.get(0).get(1)));
return filters.toString();
}
改完的版本
private static final String FILTER_OR = "firstName='%s' or lastName='%s'";
private static final String FILTER_AND = "firstName='%s' and lastName='%s'";
private static final String FILTER_OR_CON = "firstName='%s' or lastName='%s' con";
private static final String FILTER_AND_CON = "firstName='%s' and lastName='%s' con";
private static final String FILTER_USER_ID_NAME = "firstName='%s' and lastName='%s' and userId";
private static final int FIRST_NAME_INDEX = 0;
private static final int LAST_NAME_INDEX = 1;
private String filterBuilder(List<List<String>> searchLists) {
int size = searchLists.size();
if (size == 0) {
return "";
}
StringBuilder filters = new StringBuilder();
List<String> firstRow = searchLists.get(0);
String firstName = firstRow.get(FIRST_NAME_INDEX);
String lastName = firstRow.get(LAST_NAME_INDEX);
String lastRow = String.format(FILTER_USER_ID_NAME, firstName, lastName);
if (firstName.equals(lastName)) {
filters.append(String.format(FILTER_OR, firstName, lastName));
} else {
filters.append(String.format(FILTER_AND, firstName, lastName));
}
for (int i = 1; i < size; i++) {
List<String> crrRow = searchLists.get(i);
firstName = crrRow.get(FIRST_NAME_INDEX);
lastName = crrRow.get(LAST_NAME_INDEX);
if (firstName.equals(lastName)) {
filters.append(String.format(FILTER_OR_CON, firstName, lastName));
} else {
filters.append(String.format(FILTER_AND_CON, firstName, lastName));
}
}
filters.append(lastRow);
return filters.toString();
}