【转载】Java重构示例【1】

序言

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

16 减少重复计算

16.1 重构前

if(list != null && list.size() > 0){

for(int i = 0; i < list.size(); i++){

//skip...

}

}

16.2 重构后

if(list != null){

for(int i = 0, len = list.size(); i < len; i++){

//skip...

}

}

17 何时需要何时创建

17.1 重构前

public static Date parseDate(String date) throws ParseException {

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

if ((date == null) || (date.equals(""))) {

return null;

}

else {

try {

return formatter.parse(date);

} catch (ParseException pe) {

throw new ParseException(pe.getMessage(), pe.getErrorOffset());

}

}

}

17.2 重构后

public static Date parseDate(String date) throws ParseException {

if ((date == null) || (date.equals(""))) {

return null;

}

else {

try {

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

return formatter.parse(date);

} catch (ParseException pe) {

throw new ParseException(pe.getMessage(), pe.getErrorOffset());

}

}

}

18 利用已有的计算结果

18.1 重构前

public static final String DAY = "DAY";

public static final String MONTH = "MONTH";

public static final String YEAR = "YEAR";

public static final String HOUR = "HOUR";

public static final String MINUTE = "MINUTE";

public static final String SECOND = "SECOND";

public static final String WEEK = "WEEK";

public long toMilliseconds(String unit) {

if (unit == null) {

return 0L;

} else if (unit.equals(SECOND)) {

return 1 * 1000L;

} else if (unit.equals(MINUTE)) {

return 60 * 1000L;

} else if (unit.equals(HOUR)) {

return 60 * 60 * 1000L;

} else if (unit.equals(DAY)) {

return 24 * 60 * 60 * 1000L;

} else if (unit.equals(WEEK)) {

return 7 * 24 * 60 * 60 * 1000L;

} else if (unit.equals(MONTH)) {

return 30 * 24 * 60 * 60 * 1000L;

} else if (unit.equals(YEAR)) {

return 365 * 24 * 60 * 60 * 1000L;

} else {

return 0L;

}

}

18.2 重构后

public class Unit {

private static final long SECOND_MILLIS = 1000;

private static final long MINUTE_MILLIS = 60 * SECOND_MILLIS;

private static final long HOUR_MILLIS = 60 * MINUTE_MILLIS;

private static final long DAY_MILLIS = 24 * HOUR_MILLIS;

private static final long WEEK_MILLIS = 7 * DAY_MILLIS;

private static final long MONTH_MILLIS = 30 * DAY_MILLIS;

private static final long YEAR_MILLIS = 365 * DAY_MILLIS;

private static final long CENTURY_MILLIS = 100 * YEAR_MILLIS;

static Map<String, Unit> units = new HashMap<String, Unit>();

public static final Unit SECOND = new Unit(SECOND_MILLIS, "SECOND");

public static final Unit MINUTE = new Unit(MINUTE_MILLIS, "MINUTE");

public static final Unit HOUR = new Unit(HOUR_MILLIS, "HOUR");

public static final Unit DAY = new Unit(DAY_MILLIS, "DAY");

public static final Unit WEEK = new Unit(WEEK_MILLIS, "WEEK");

public static final Unit MONTH = new Unit(MONTH_MILLIS, "MONTH");

public static final Unit YEAR = new Unit(YEAR_MILLIS, "YEAR");

public static final Unit CENTURY = new Unit(CENTURY_MILLIS, "CENTURY");

static {

units.put(SECOND.name, SECOND);

units.put(MINUTE.name, MINUTE);

units.put(HOUR.name, HOUR);

units.put(DAY.name, DAY);

units.put(WEEK.name, WEEK);

units.put(MONTH.name, MONTH);

units.put(YEAR.name, YEAR);

units.put(CENTURY.name, CENTURY);

}

private long millis;

private String name;

private Unit(long millis, String name) {

this.millis = millis;

this.name = name;

}

public long getMillis() {

return millis;

}

public String getName() {

return name;

}

public String toString() {

StringBuffer buffer = new StringBuffer();

buffer.append(this.getName());

buffer.append("[");

buffer.append(this.getMillis());

buffer.append("]");

return buffer.toString();

}

}

public long toMilliseconds(Unit unit) {

return unit.getMillis();

}

19 替换switch结构

19.1 重构前

public boolean isLeap(int year) {

return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);

}

public static int getMonthDays(int year, int month) {

int numberDays = 0;

switch (month) {

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

numberDays = 31;

break;

case 4:

case 6:

case 9:

case 11:

numberDays = 30;

break;

case 2:

numberDays = isLeap(year) ? 29 : 28;

break;

}

return numberDays;

}

19.2 重构后

public boolean isLeap(int year) {

return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);

}

private int getFebruaryDays(int year) {

return this.isLeap(year) ? 29 : 28;

}

public int getMonthDays(int year, int month) {

int[] months = new int[] { 31, this.getFebruaryDays(year), 31, 30, 31, 30,

31, 31, 30, 31, 30, 31 };

return months[month];

}

20 避免对参数赋值

20.1 重构前

public Date getStartTime(Date date) {

date.setMinutes(fromMinute);

date.setDate(fromHour);

date.setDate(fromHour);

date.setSeconds(0);

return date;

}

20.2 重构后

public Date getStartTime(final Date date) {

Calendar calendar = Calendar.getInstance();

calendar.setTime(date);

calendar.set(Calendar.HOUR_OF_DAY, fromHour);

calendar.set(Calendar.MINUTE, fromMinute);

calendar.set(Calendar.SECOND, 0);

return calendar.getTime();

}

<!--EndFragment-->

源地址:http://passport.baidu.com/?business&aid=6&un=%B2%DC%CB%C9%C7%E0#7

上一篇:FireMonkey 源码学习(1)


下一篇:UVA 11100 The Trip, 2007 贪心(输出比较奇葩)