项目上要基于现有代码开发,却碰到了很多让人蛋疼的代码。例如下面这个,大家看看能找到多少槽点:
public static String addDate(String date, String into) throws Exception
{
String Sdate = "";
try
{
date = date.replaceAll("-", "/");
date = date.substring(0, date.length() - 2);
GregorianCalendar grc = new GregorianCalendar();
grc.setTime(new Date(date));
grc.add(GregorianCalendar.DATE, Integer.parseInt(into));
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Sdate = new String(formatter.format(grc.getTime()));
}
catch (Exception e)
{
throw e;
}
return Sdate;
}
1. catch了exception又抛出去,addDate本来就要抛出Exception,try纯属多余
2. Sdate变量纯属多余
3. formatter.format本来就返回一个String,又new一个String,纯属多余