|
|
code rule: 返回值尽量不用null |
|
|
作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站 |
当我们使用一个对象的时候,往往需要检查这个对象是否为null, 因为直接使用一个null对象,会抛出一个NullPointerException, 要检查,又必须增加一个check代码,比较麻烦
在项目中,为了避免这个情况,底层模块向invoker方返回的数据 ,尽量为空,而不用null,这样就可以避免错误,让系统更加稳定。
for example: -------------INVOKER-------------- public void getValueFromSubSystem() { String a = getValue(); System.out.println(a); long[] array = getArray(); for(int i=0;i<array.length;i++) System.out.println(array[i]);
}
-----------BACKEND--------------- public String getValue() { if(isNull()) return ““; //empty string to avoid checking null value //omitted ............
}
public long[] getArray() { if(isNull()) return new long[0]; //empty array to avoid checking null value //omitted ............ }
It is a good way to make system robust. It should be adopted in projects.
What about your opinion?

|
|
相关文章:相关软件: |
|