@ZeroGeek
2016-07-18T08:49:04.000000Z
字数 4896
阅读 1672
未分类
public static boolean isEmpty(Collection<?> collection){
return collection == null || collection.isEmpty();
}
public static boolean isEmpty(Map map){
return map == null || map.isEmpty();
}
double d = 1.1;
BigDecimal bd1 = new BigDecimal(d); // Noncompliant; see comment above
BigDecimal bd2 = new BigDecimal(1.1); // Noncompliant; same result
Compliant Solution
double d = 1.1;
BigDecimal bd1 = BigDecimal.valueOf(d);
BigDecimal bd2 = BigDecimal.valueOf(1.1);
private void closeCursor(Cursor c) {
if (c != null) {
if (!c.isClosed()) {
c.close();
c = null; // 多余
}
} else {
c = null; // 多余
}
}
private void delShareAccBook(AccountBookVo shareAccBook){
if(shareAccBook!=null){
try {
AccountBookManager.getInstance().deleteAccountBook(shareAccBook);
} catch (AccountBookException e) {
DebugUtil.exception(TAG, e);
}
shareAccBook = null; // 多余
}
}
public void doSomethingWithMap(Map<String,Object> map) {
for (String key : map.keySet()) { // Noncompliant; for each key the value is retrieved
Object value = map.get(key);
// ...
}
}
public void doSomethingWithMap(Map<String,Object> map) {
for (Map.Entry<String,Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
// ...
}
}
public static CreditsListener creditsListener; // 暴露了接口
BaseApplication.java
/**
* 全局缓存,终端修改 删除新增账单均要填写ffrom字段,内容是终端类型+产品名称+版本号+"操作类型",交易增删改时填写
*/
public static String TRANS_FFROM_INSERT = "";
public static String TRANS_FFROM_UPDATE = "";
public static String TRANS_FFROM_DELETE = "";
// 这种情况,建议提供静态方法对其读取或者复制
抽象类命名 “AbstractXXX”
Looks for methods named "getX()" with "boolean" as the return type. The convention is to name these methods "isX()".
Catches should be combined
catch (IOException e) {
doCleanup();
logger.log(e);
}
catch (SQLException e) { // Noncompliant
doCleanup();
logger.log(e);
}
catch (TimeoutException e) { // Compliant; block contents are different
doCleanup();
throw e;
}
Compliant Solution
catch (IOException|SQLException e) {
doCleanup();
logger.log(e);
}
catch (TimeoutException e) {
doCleanup();
throw e;
}
if语句后加括号,哪怕是一句
"BigDecimal(double)" should not be used
public static void main(String[] args) {
BigDecimal d1 = new BigDecimal(0.1);
BigDecimal d2 = BigDecimal.valueOf(0.1); // 推荐用法
if (d1.equals(d2)) {
System.out.print("==");
} else {
System.out.print("!="); // 执行结果
}
}
"enum" fields should not be publicly mutable
错误用法:公有成员变量,公有的set方法
"equals" methods should be symmetric and work for subclasses
if (this.getClass() == obj.getClass()) {
return ripe.equals(((Fruit)obj).getRipe());
}
// Noncompliant Code Example
class StringUtils { // Noncompliant
public static String concatenate(String s1, String s2) {
return s1 + s2;
}
}
// Compliant Solution
class StringUtils { // Compliant
private StringUtils() {
throw new IllegalAccessError("Utility class");
}
public static String concatenate(String s1, String s2) {
return s1 + s2;
}
}
方法的参数建议用可变列表参数 替代 数组参数
预防空指针引起漰溃
78 处, 以下是示例
@CheckForNull
String getName(){...}
public boolean isNameEmpty() {
return getName().length() == 0; // Noncompliant; the result of getName() could be null, but isn't null-checked
}
Connection conn = null;
Statement stmt = null;
try{
conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.createStatement();
// ...
}catch(Exception e){
e.printStackTrace();
}finally{
stmt.close(); // Noncompliant; stmt could be null if an exception was thrown in the try{} block
conn.close(); // Noncompliant; conn could be null if an exception was thrown
}
private void merge(@Nonnull Color firstColor, @Nonnull Color secondColor){...}
public void append(@CheckForNull Color color) {
merge(currentColor, color); // Noncompliant; color should be null-checked because merge(...) doesn't accept nullable parameters
}
float a = 16777216.0f;
float b = 1.0f;
float c = a + b; // Noncompliant; yields 1.6777216E7 not 1.6777217E7
double d = a + b; // Noncompliant; addition is still between 2 floats
float a = 16777216.0f;
float b = 1.0f;
BigDecimal c = BigDecimal.valueOf(a).add(BigDecimal.valueOf(b));
double d = (double)a + (double)b;
Mutable fields should not be "public static"
集合类的成员变量应该设置为 protected 或者 private 而不是 public static
Mutable members should not be stored or returned directly
class A {
private String [] strings;
public A () {
strings = new String[]{"first", "second"};
}
public String [] getStrings() {
return strings; // Noncompliant
}
public void setStrings(String [] strings) {
this.strings = strings; // Noncompliant
}
}
public class B {
private A a = new A(); // At this point a.strings = {"first", "second"};
public void wreakHavoc() {
a.getStrings()[0] = "yellow"; // a.strings = {"yellow", "second"};
}
}