中间变量
Matcher matcher = headerPattern.matcher(line);
if (matcher.find()) {headers.put(matcher.group(1), matcher.group(2));
}
优化后
Matcher matcher = headerPattern.matcher(line);
if (matcher.find()) {String key = matcher.group(1);String value = matcher.group(2);headers.put(key, value);
}
设计模式语言
下面是一个典型pipeline处理方式,责任链在处理该问题上是一个很好的选项,FilterChain 这个名字非常恰当地表达出了意图,Chain 表示用的是责任链模式,Filter 表示用来进行过滤。
FilterChain filterChain = FilterChainFactory.buildFilterChain(NoEmailAddressFilter.class,EmailUnsubscribeFilter.class,EmailThreeDayNotRepeatFilter.class);// 具体的Filter
public class NoEmailAddressFilter implements Filter {public void doFilter(Object context, FilterInvoker nextFilter) {Map<String, Object> contextMap = (Map<String, Object>) context;String email = ConvertUtils.convertParamType(contextMap.get("email"), String.class);if (StringUtils.isBlank(email)) {return;}nextFilter.invoke(context);}}
注释不要复述功能
public synchronized void setFormatter(Formatter newFormatter) {checkPermission();// Check for a null pointer.newFormatter.getClass();formatter = newFormatter;
}
注释要解释背后意图
try {// 这里等待2 秒.Thread.sleep(2000L);
} catch (Exception e) {LOGGER.error(e);
}
优化后
try {// 等待2 秒,为了等待关联系统处理结果Thread.sleep(2000L);
} catch (Exception e) {LOGGER.error(e);
}