Java重构示例三

Java重构示例三
关键字:Java 程序设计 重构 示例 原则 优化 技巧 方法
序言
本文通过Java示例代码片段展示了常用重构原则和技巧,供初级开发人员参考。精致的代码能够清楚传达作者的意图,精致的代码是最好的注释,精致的代码非常容易维护和扩展。程序员阅读精致的代码如同大众欣赏优美的散文一样享受。
11    使方法通用化
11.1    重构前

public DayPart[] sortDayPart() {
  if (dayParts == null || dayParts.length == 0) {
    return dayParts;
  }
  List<DayPart> dayPartList = Arrays.asList(dayParts);

  Collections.sort(dayPartList, new Comparator<DayPart>() {
    public int compare(DayPart o1, DayPart o2) {
      if (o1.getIndex() < o2.getIndex()) {
        return -1;
      } else if (o1.getIndex() > o2.getIndex()) {
        return 1;
      } else {
        return 0;
      }
    }
  });

  return dayPartList.toArray(new DayPart[dayPartList.size()]);
}

11.2    重构后

public DayPart[] sortDayPart() {
  return this.sortDayPart(SortMode.ASC);
}

public DayPart[] sortDayPart(final SortMode sortMode) {
  if (dayParts == null || dayParts.length == 0) {
    return dayParts;
  }
  List<DayPart> dayPartList = Arrays.asList(dayParts);

  Collections.sort(dayPartList, new Comparator<DayPart>() {
    public int compare(DayPart o1, DayPart o2) {
      if (o1.getIndex() < o2.getIndex()) {
        return sortMode.isAsc() ? -1 : 1;
      } else if (o1.getIndex() > o2.getIndex()) {
        return sortMode.isAsc() ? 1 : -1;
      } else {
        return 0;
      }
    }
  });

  return dayPartList.toArray(new DayPart[dayPartList.size()]);
}

12    避免空语句
12.1    重构前

public int getRemainMinutes(int hour, int minute) {
  int startHour = fromHour;
  int startMinute = fromMinute;
  if (this.fromAfterEqual(hour, minute)) {
    // ------from-------position------to--------
    ;//use default init value
  } else if (this.toAfterEqual(hour, minute)) {
    // ------from-------position------to--------
    startHour = hour;
    startMinute = minute;
  } else {
    // --------from-------to--------position-------
    startHour = toHour;
    startMinute = toMinute;
  }

  return this.getMinutes(startHour, startMinute, toHour, toMinute);
}

12.2    重构后

public int getRemainMinutes(int hour, int minute) {
  // --------from-------to--------position-------
  int startHour = toHour;
  int startMinute = toMinute;

  if (this.fromAfterEqual(hour, minute)) {
    // ------position------from-------to--------
    startHour = fromHour;
    startMinute = fromMinute;
  } else if (this.toAfterEqual(hour, minute)) {
    // ------from-------position------to--------
    startHour = hour;
    startMinute = minute;
  }

  return this.getMinutes(startHour, startMinute, toHour, toMinute);
}

13    公共逻辑后置
13.1    重构前

public int getRemainMinutes(int hour, int minute) {
  if (this.fromAfterEqual(hour, minute)) {
    // ------position------from-------to--------
    return (toHour * 60 + toMinute) - (fromHour * 60 + fromMinute);
  } else if (this.toAfterEqual(hour, minute)) {
    // ------from-------position------to--------
    return (toHour * 60 + toMinute) - (hour * 60 + minute);
  }
  else {
    // --------from-------to--------position-------
    return 0; //(toHour * 60 + toMinute) - (toHour * 60 + toMinute);
  }
}

13.2    重构后

private int getMinutes(int startHour, int startMinute, int endHour,
            int endMinute) {
  int minutes = 0;
  minutes = (endHour * 60 + endMinute) - (startHour * 60 + startMinute);

  return minutes;
}

public int getRemainMinutes(int hour, int minute) {
  // --------from-------to--------position-------
  int startHour = toHour;
  int startMinute = toMinute;

  if (this.fromAfterEqual(hour, minute)) {
    // ------position------from-------to--------
    startHour = fromHour;
    startMinute = fromMinute;
  } else if (this.toAfterEqual(hour, minute)) {
    // ------from-------position------to--------
    startHour = hour;
    startMinute = minute;
  }

  return this.getMinutes(startHour, startMinute, toHour, toMinute);
}

14    公共逻辑前置
14.1    重构前

private StringBuffer transformDialect(String intPart, String fracPart) {
  StringBuffer cn = new StringBuffer();

  if (isIntegerZero(intPart)) {
    return cn;
  }

  if (this.isFractionZero(fracPart)) {
// 无小数,增加附加信息(整)
    cn.append("元").append("整");
  } else {
// 有小数,增加附加信息(零)
    cn.append("元").append("零");
  }

  return cn;
}

14.2    重构后

private StringBuffer transformDialect(String intPart, String fracPart) {
  StringBuffer cn = new StringBuffer();

  if (isIntegerZero(intPart)) {
    return cn;
  }

  cn.append("元");
  if (this.isFractionZero(fracPart)) {
// 无小数,增加附加信息(整)
    cn.append("整");
  } else {
// 有小数,增加附加信息(零)
    cn.append("零");
  }

  return cn;
}

15    使用清晰的布尔型变量替换逻辑表达式
15.1    重构前

public boolean tailGreatHead(int headHour, int headMinute, int tailHour,
            int tailMinute, boolean includeEqual) {

  return ((headHour < tailHour) ||
 ((headHour == tailHour) &&
           ((headMinute < tailMinute) || includeEqual && (headMinute == tailMinute))));
}


15.2    重构后

public boolean tailGreatHead(int headHour, int headMinute, int tailHour,
            int tailMinute, boolean includeEqual) {
  boolean tailGreatHeadHour = (headHour < tailHour);
  boolean tailEqualHeadHour = (headHour == tailHour);
  boolean tailGreatHeadMinute = (headMinute < tailMinute);
  boolean tailEqualHeadMinute = (headMinute == tailMinute);

  boolean tailGreatEqualHeadMinute = tailGreatHeadMinute || includeEqual
                && tailEqualHeadMinute;

  return (tailGreatHeadHour || (tailEqualHeadHour && tailGreatEqualHeadMinute));
}


posted on 2012-08-17 10:01  要么牛逼,要么滚蛋  阅读(498)  评论(0编辑  收藏  举报

导航