【仅供内部供应商使用,不提供对外解答和培训】
【仅供内部供应商使用,不提供对外解答和培训】
命名要能大致体现出方法的功能,不能使用一个简单的单词。
1.类命名:首字母大写,其他单词除首字母大写外都使用小写(除开一些特殊单词,如URL,HTML这种)
2.方法名:首字母小写,其他单词除首字母大写外都使用小写
3.变量名:首字母小写,其他单词除首字母大写外都使用小写
public class DateWork { private int remainDays = 10;//剩余的天数 public void calculateNameWithData(Data data) { } }
两个对象做equals判断时,必须使用ComparatorUtils.equals(Object obj1, Object obj2)来进行判断,可以避免各种空指针错误。如果是明确的字符串比较,可以不使用工具方法。
if (a.equals(b)) { // do something }
if (ComparatorUtils.equals(a, b)) { // do something }
下面的写法同样正确,并且是推荐的写法,因为字符串"abcde"是确定不为null的:
if ("abcde".equals(op)) { // do something }
任意一个方法的长度不得超过50行。过长的方法不方便写单元测试后期的维护以及扩展,尽量做到一个方法只做一件单一的事情。
公共API方法的参数不要超过4个,过多的参数不利于方法的理解和使用。除了关于HTTP请求之类的本身就带了Response和Request两个参数的方法除外。
if,if-else,for,while,do和switch语句后面必须跟上"大括号{}"。主要是为了清晰的表名语句的作用范围和避免一些低级的错误。
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);