不要在循环中创建变量Avoid instantiating new objects inside loops
private static List<MetricDataPojo> buildeMetricData(final String monitorId, final StateType stateType) { List<MetricDataPojo> result = new ArrayList<MetricDataPojo>(); for (RoomPojo room : ResCache. getRoomCache ().values()) { if (!room.getMonitorId().equals(monitorId)) { continue; } MetricDataPojo metricData = new MetricDataPojo(); metricData.setMetricId("ping"); metricData.setMonitorId(monitorId); metricData.setRoomId(room.getId()); metricData.setValue(stateType.getId()); metricData.setTime( new Date() ); result.add(metricData); } return result; } |
避免过早定义变量【定义靠近使用的地方】Avoid declaring a variable if it is unreferenced before a possible exit point.
Variables that are final and static should be all capitals, 'logger' is not all
private static final Logger logger = LoggerFactory.getLogger(CacheManager.class); |
Use block level rather than method level synchronization
public synchronized Cache registerCache( |
Local variable 'serviceLoader' could be declared final
CacheLoader<Class<S>, S> serviceLoader = new CacheLoader<Class<S>, S>() |
A class which only has private constructors should be final
App.java
Avoid variables with short names like 'sb'
Avoid if (x != y) ..; else ..;
JUnit tests should include assert() or fail()
publicMethodCommentRequirement Required
StringBuffer链式调用
StringBuffer (or StringBuilder).append is called consecutively without reusing the target variable.
默认16
StringBuffer constructor is initialized with size 16, but has at least 417 characters appended.
ModelServiceGenerator.java
Substitute calls to size() == 0 (or size() != 0) with calls to isEmpty()【区别是?】
System.out.print is used
for (File file : files) { System.out.println(file.getName()); } |
先写正常流Avoid if (x != y) ..; else ..;
if (existNode != null) { node.setId(existNode.getId()); if (!existNode.equals(node)) { service.modifySelective(node); } } else { toAdd.add(node); } |
SimplifyBooleanReturns: Avoid unnecessary if..then..else statements when returning a boolean.
解决方案 简化布尔量的返回:避免在返回布尔量时写不必要的if..then..else表达式。
代码示例:
public class Foo {
private int bar =2;
public boolean isBarEqualsTo(int x) {
// this bit of code
if (bar == x) {
return true;
} else {
return false;
}
// 上面可以简化为:
// return bar == x;
}
}