【仅供内部供应商使用,不提供对外解答和培训】
...
Code Block |
---|
public class DateWork { private int remainDays = 10;//剩余的天数 public void calculateNameWithData(Data data) { } } |
Note |
---|
两个对象做equals判断时,必须使用ComparatorUtils.equals(Object obj1, Object obj2)来进行判断,可以避免各种空指针错误。如果是明确的字符串比较,可以不使用工具方法。 |
Code Block | |
---|---|
|
...
| |
if (a.equals(b)) {
// do something
} |
...
Code Block | ||
---|---|---|
| ||
if (ComparatorUtils.equals(a, b)) {
// do something
} |
同样正确:下面的写法同样正确,并且是推荐的写法,因为字符串"abcde"是确定不为null的:
Code Block | ||
---|---|---|
| ||
if |
...
("abcde".equals(op)) {
// do something
} |
Note |
---|
任意一个方法的长度不得超过50行。过长的方法不方便写单元测试后期的维护以及扩展,尽量做到一个方法只做一件单一的事情。 |
Note |
---|
公共API方法的参数不要超过4个,过多的参数不利于方法的理解和使用。除了关于HTTP请求之类的本身就带了Response和Request两个参数的方法除外。 |
Note |
---|
if,if-else,for,while,do和switch语句后面必须跟上“大括号{}”。主要是为了清晰的表名语句的作用范围和避免一些低级的错误。 |
Code Block |
---|
...
| ||
if (statement) return; |
...
Code Block | ||
---|---|---|
| ||
if (statement) { |
...
return; } |
Note |
---|
尽量使程序结构能更正确简洁的表达你的意图。如果能不用条件判断,则不要使用条件判断。 |
Code Block | |
---|---|
|
...
| |
if (booleanExpression) {
return true;
} else {
return false;
} |
...
Code Block | ||
---|---|---|
| ||
return booleanExpression; |
Note |
---|
严格控制方法/变量的权限,只在当前类使用的方法/变量,必须使用private修饰符。 |
...
静态变量——实例变量
public变量——protected变量——private变量不推荐:
Code Block | ||
---|---|---|
| ||
public class Test {
protected String myProtectedData = "Protected";
public static final String AAA = "aaa";
private String myPrivate = "Private";
public String myData = "data";
} |
...
Code Block | ||
---|---|---|
| ||
public class Test {
public static final String AAA = "aaa";
public String myData = "data";
protected String myProtectedData = "Protected";
private String myPrivate = "Private";
} |
Note |
---|
禁止在类中出现魔术数,应该使用常量(static final)来替代。这种方式通过合适的常量名可以让其他人能读懂该魔术数的意义。 |
Code Block |
---|
...
| ||
public double area(double r) {
return 3.14 * r *r;
} |
Code Block | |
---|---|
|
...
| |
private static final int PI = 3.14;
public double area(double r) {
return PI * r * r;
} |
Note |
---|
如果包含二元运算的表达式出现在三元运算符的“?”之前,应该用括号将他括起来。 |
...
Code Block | ||
---|---|---|
| ||
return a + b > 0 ? "student" : "teacher"; |
...
Code Block | ||
---|---|---|
| ||
return (a + b > 0) ? "student" : "teacher"; |
Note |
---|
常量命名必须全部大写,单词之间用下划线分割。 |
...
同时需要注意的是:如果常量是写在接口中的,那么忽略掉所有的修饰符。
Code Block | ||
---|---|---|
| ||
public static final String WidgetName = "WidgetName"; |
Code Block |
---|
...
| ||
public static final String WIDGET_NAME = "WidgetName"; |
...
...
Note |
---|
用于设置对象属性的方法前缀必须是set,用于获取一个布尔对象属性的方法前缀必须是is/has,而用于获取其他类型属性的方法前缀必须是get。 |
Code Block | ||
---|---|---|
| ||
public class Student {
private boolean male;
public boolean getMale() {
return male;
}
} |
Code Block |
---|
...
| ||
public class Student {
private boolean male;
public boolean isMale() {
return male;
}
} |
Note |
---|
不要向构造函数和公共方法中传递null参数。null参数很难直接从方法声明上去理解该参数的意义。 |
假设有如下的工具方法:
Code Block | ||
---|---|---|
| ||
public class Utils {
public static Border createBorder(Font font, Color color) {
if (color == null) {
color = new Color(223, 122,123);
}
}
} |
...
Code Block | ||
---|---|---|
| ||
Border border = Utils.createBorder(font, null); |
...
Code Block | ||
---|---|---|
| ||
//先在Utils里面加这么个方法:
public static Border createBorder(Font font) {
return createBorder(font, null);
}
//再调用
Border border = Utils.createBorder(font); |
Note |
---|
在重写方法时,禁止出现下面的写法,即只简单的调用了一下super而没有额外的实现, 这里有一个例外,就是实现了FClone接口的类必须要实现clone方法,和该规则冲突,不用管。 |
...
Code Block | ||
---|---|---|
| ||
@override
public void doSomething() {
super.doSomething();
} |
Note |
---|
代码中出现的中文(英文)等字符,必须做国际化。 |
...
Code Block | ||
---|---|---|
| ||
String shapeName = "圆形"; |
Code Block |
---|
...
| ||
String shapeName = Inter.getLocText("FR-Designer_Chart_Circle"); |
...
Note |
---|
包名要具有描述意义,而且包中的类需要严格按照包描述的意义进行放置,另外,包名都必须是由小写字母组成。 |
比如com.fr.chart.axis包中的所有类都必须是和坐标轴相关的类。
Note |
---|
数组的声明总是采用统一的方式。 |
...
Code Block | ||
---|---|---|
| ||
String strArray[]; |
Code Block | |
---|---|
|
...
| |
String[] strArray; |
Note |
---|
如果一个方法的返回值是数组,那么就必须返回一个数组,如果是空值就使用空数组的代替。 |
Code Block | ||
---|---|---|
| ||
public String[] getNames4JionTheParty() {
if (a) {
return new String[] { |
...
"张三", |
...
"李四", |
...
"王麻子"}; } else (b) { return new String[0]; } } |
Note |
---|
在需要条件判断的地方,比如if语句里不要写过长的表达式(不超过两个逻辑表达式),此时应该用一个方法来替代表达式。 |
Code Block |
---|
...
| ||
if (a > -1 && a != 1 && dim.width > 0 && dim.height > 0) {
doSomething();
} |
Code Block | |
---|---|
|
...
| |
if (shouldDoSomething(a, dim)) {
doSomething();
}
boolean shuoldDoSomething(int a, Dimension dim) {
return a > -1 && a != 1 && dim > width && dim.height > 0;
} |
Note |
---|
使用HashMap和HashSet以及HashTable等时要注意,如果使用的自定义的对象作为key,需要同时重写该对象的equals和hashCode方法。 |
Note |
---|
变量的声明应当尽可能靠近其使用的位置。局部变量应该在方法顶部出现,而循环中的控值变量应该总是在循环语句中声明。 |
Note |
---|
当switch的一个case语句没有break时,需要在case结尾处写上注释,防止是漏掉了break语句而不是原本就是想贯穿的。 |
Code Block | ||
---|---|---|
| ||
public String diffResult(int type) {
String someDescription = "abc";
switch (type) {
// 这里需要贯穿
case 1:
someDescription = "def";
case 2:
someDescription = "xyz";
break;
default:
someDescription = "mnx";
break;
}
return someDescription;
} |
Note |
---|
异常信息的日志写法要符合规范。 |
Code Block | |
---|---|
|
...
| |
try {
doSomething();
} catch (Exception e) {
e.printStackTrace();
} |
...
Code Block | ||
---|---|---|
| ||
try {
doSomething();
} catch (Exception e) {
FRContext.getLogger.error(e.getMessage, e);// 这里调用的方法视情况而定
} |
Note |
---|
如果需要忽略掉异常信息,请显示的将异常对象命名为 ignore。 |
...
Code Block | ||
---|---|---|
| ||
try {
doSomething();
} catch (Exception ignore) {
} |
Note |
---|
在Java代码中需要输出JSON格式时,JSON字符串需要按照标准写。 |
Code Block | ||
---|---|---|
| ||
{a:"bb"};
{'a':"bb"}; |
Code Block | |
---|---|
|
...
| |
{"aa":"bb"}; |
Note |
---|
在写需要输出到客户端的JSON对象时,如果需要输出数组对象,不要使用普通的Java数组对象,要使用JSONArray对象。 |
Code Block | |
---|---|
|
...
| |
JSONObject jo = |
...
JSONObject.create(); String[] names = new String[]{"aaa", "bbb", "ccc"}; jo.put("names", names); |
Code Block | |
---|---|
|
...
| |
JSONObject jo = |
...
JSONObject.create(); JSONArray ja = new JSONArray(); ja.put("aaa").put("bbb").put("ccc"); jo.put( |
...
"names", ja); |
Note |
---|
如果需要使用空字符串,不要直接使用"",用StringUtils.EMPTY。 |
Code Block | ||
---|---|---|
| ||
if (name == null) { return "";// 不正确 } if (name == null) { return StringUtils.EMPTY;// 正确 } |
Note |
---|
判断字符串是否为空,也要使用StringUtils#isEmpty(String); |
Code Block |
---|
String name = createByAge(20);
if ("".equals(name)) {// 不正确
}
if (StringUtils.isEmpty(name)) { // 正确
} |
Note |
---|
将一个对象转化为字符串,不能直接使用toString方法,需要使用GeneralUtils#objectToString(Object)方法。 |
Code Block |
---|
Key key = getKey();
String result = key.toString();// 错误,无法避免空指针等错误
String okStr = GeneralUtils.objectToString(key);// 正确 |
Note |
---|
从HTTP请求中获取参数的时候,需要使用NetworkHelper#getHTTPRequestParameter(HttpServletRequest,String)方法。 |
不用该直接用HttpServletRequest#getParameter(String)方法,这个方法会导致漏掉参数护着无法正确解码。