【仅供内部供应商使用,不提供对外解答和培训】
【仅供内部供应商使用,不提供对外解答和培训】
命名要能大致体现出方法的功能,不能使用一个简单的单词。
不推荐:
if (a.equals(b)) { // do something }
正确:
if (ComparatorUtils.equals(a, b)) { // do something }
同样正确:
if ("abcde".equals(op)) { // do something }
不推荐:
if (statement) return;
正确:
if (statement) { return; }
不推荐:
if (booleanExpression) { return true; } else { return false; }
正确:
return booleanExpression;
静态变量——实例变量
public变量——protected变量——private变量
不推荐:
public class Test { protected String myProtectedData = "Protected"; public static final String AAA = "aaa"; private String myPrivate = "Private"; public String myData = "data"; }
正确:
public class Test { public static final String AAA = "aaa"; public String myData = "data"; protected String myProtectedData = "Protected"; private String myPrivate = "Private"; }
不推荐:
public double area(double r) { return 3.14 * r *r; }
正确:
private static final int PI = 3.14; public double area(double r) { return PI * r * r; }
不推荐:
return a + b > 0 ? "student" : "teacher";
正确:
return (a + b > 0) ? "student" : "teacher";
不推荐:
public static final String WidgetName = "WidgetName";
正确:
public static final String WIDGET_NAME = "WidgetName";
不推荐:
public class Student { private boolean male; public boolean getMale() { return male; } }
正确:
public class Student { private boolean male; public boolean isMale() { return male; } }
假设有如下的工具方法:
public class Utils { public static Border createBorder(Font font, Color color) { if (color == null) { color = new Color(223, 122,123); } } }
不推荐:
Border border = Utils.createBorder(font, null);
正确:
//先在Utils里面加这么个方法: public static Border createBorder(Font font) { return createBorder(font, null); } //再调用 Border border = Utils.createBorder(font);
不推荐:
@override public void doSomething() { super.doSomething(); }
错误:
String shapeName = "圆形";
正确:
String shapeName = Inter.getLocText("Circle");
不推荐:
String strArray[];
正确:
String[] strArray;
public String[] getNames4JionTheParty() { if (a) { return new String[] {“张三”, “李四”, “王麻子”}; } else (b) { return new String[0]; } }
不推荐:
if (a > -1 && a != 1 && dim.width > 0 && dim.height > 0) { doSomething(); }
正确:
if (shouldDoSomething(a, dim)) { doSomething(); } boolean shuoldDoSomething(int a, Dimension dim) { return a > -1 && a != 1 && dim > width && dim.height > 0; }
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; }
不推荐:
try { doSomething(); } catch (Exception e) { e.printStackTrace(); }
正确:
try { doSomething(); } catch (Exception e) { FRContext.getLogger.error(e.getMessage, e);// 这里调用的方法视情况而定 }
错误:
{a:"bb"}; {'a':"bb"};
正确:
{"aa":"bb"};
错误:
JSONObject jo = new JSONObject(); String[] names = new String[]{"aaa", "bbb", "ccc"}; jo.put("names", names);
正确:
JSONObject jo = new JSONObject(); JSONArray ja = new JSONArray(); ja.put("aaa").put("bbb").put("ccc"); jo.put(“names”, ja);