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