IDE 配置模板文件說明:
文檔:java代碼格式化模板(阿里代碼規(guī)范)
地址:http://www.reibang.com/p/9befe7710176
基于項(xiàng)目需要,調(diào)研規(guī)范java格式翘狱,基于 Alibaba P3C 和 Google 做一個簡單對比
Google:
文檔:https://google.github.io/styleguide/javaguide.html
源碼:https://github.com/google/google-java-format
安裝:Idea或Eclipse插件
安裝文檔:無需文檔疆前,裝插件就好
阿里巴巴:
文檔&源碼:https://github.com/google/google-java-format
安裝:Idea插件 + eclipse-code-format.xml 模板文件
安裝文檔:http://www.reibang.com/p/9befe7710176
優(yōu)缺點(diǎn)對比
配置:
Google: 縮進(jìn)轰驳、換行之類目前全是代碼里固定的简卧,不提供配置文件槽畔,自定義支持不友好哲银。不過目前已經(jīng)很好看了
Alibaba: 基于 eclipse-codestyle.xml 莱没,配置較靈活初肉,同時(shí)也很麻煩
具體代碼對比示例(自行決定使用哪一個)
行開始縮進(jìn) Google 2 Alibaba 4
換行縮進(jìn) 都是4
-
單行塊注釋
Google: /** 外部估值數(shù)據(jù) */ Alibaba: /** * 外部估值數(shù)據(jù) */
-
換行邏輯:
Google 優(yōu)先 "=" 后換行,"." "(" 后換行 饰躲,優(yōu)先級從左往右:= . (
Alibaba 到哪哪換行Google: private static final Set<String> ADJUSTSTS_TODETAIL = new HashSet<>( Arrays.asList( "1002", "1021", "1031", "1221", "1207", "2001", "2203", "2204", "2221", "2232", "3003")); Alibaba: private static final Set<String> ADJUSTSTS_TODETAIL = new HashSet<>(Arrays.asList("1002", "1021", "1031", "1221", "1207", "2001", "2203", "2204", "2221", "2232", "3003"));
-
代碼塊換行:Google 在 "{" 后換行
Google: private static final String[][] ADJUSTSTS_4001 = { {"104", "400101"}, {"104A", "40010A"}, {"104B", "40010B"}, {"104C", "40010C"} }; Alibaba: private static final String[][] ADJUSTSTS_4001 = {{"104", "400101"}, {"104A", "40010A"}, {"104B", "40010B"}, {"104C", "40010C"} };
-
參數(shù)換行:Google 在 "(" 后換行牙咏,如果1行不足,會每個單數(shù)獨(dú)占一行 (方法定義和調(diào)用一致)
Google: public ValuationCheckProcess_Google( int productNum, long tradeDate, boolean needCallBack, boolean processCheckFlag) {} Alibaba: public ValuationCheckProcess_AlibabaP3C(int productNum, long tradeDate, boolean needCallBack, boolean processCheckFlag) {} Google: public ValuationCheckProcess_Google( int productNum, long tradeDate, Map<String, BSide_EV_Valuation> valuationMap, Map<String, BSide_EV_ValuationTotal> valuationTotalMap) {} Alibaba: public ValuationCheckProcess_AlibabaP3C(int productNum, long tradeDate, Map<String, BSide_EV_Valuation> valuationMap, Map<String, BSide_EV_ValuationTotal> valuationTotalMap) {} Google: return ProcessUtil.getProcessStatus( tradeDate, productNum, isAsset ? EvaluationConst.ASSET_EV_PROCESS : EvaluationConst.EV_PROCESS_CHECK, EvaluationConst.EV_Process_check_2, EvaluationConst.ev_status_3); Alibaba: return ProcessUtil.getProcessStatus(tradeDate, productNum, isAsset ? EvaluationConst.ASSET_EV_PROCESS : EvaluationConst.EV_PROCESS_CHECK, EvaluationConst.EV_Process_check_2, EvaluationConst.ev_status_3);
-
Lambda表達(dá)式:
Google: uncheckOuterData = uncheckOuterData.entrySet().stream() .filter(map -> !map.getKey().startsWith(defineL1)) .collect(Collectors.toMap(h -> h.getKey(), h -> h.getValue())); Alibaba: uncheckOuterData = uncheckOuterData.entrySet().stream() .filter(map -> !map.getKey().startsWith(defineL1)) .collect(Collectors.toMap(h -> h.getKey(), h -> h.getValue())); Google: uncheckOuterData = uncheckOuterData.entrySet().stream() .filter( map -> map.getKey().length() > 4 || (map.getKey().length() == 4 && String.valueOf(map.getKey().charAt(3)).matches("[0-9]"))) .collect(Collectors.toMap(h -> h.getKey(), h -> h.getValue())); Alibaba: uncheckOuterData = uncheckOuterData.entrySet().stream() .filter(map -> map.getKey().length() > 4 || (map.getKey().length() == 4 && String.valueOf(map.getKey().charAt(3)).matches("[0-9]"))) .collect(Collectors.toMap(h -> h.getKey(), h -> h.getValue())); Google: Map<String, BSide_EV_Subject> filterSubjects = allSubjects.values().stream() .filter( e -> !ParaChecker.isNull(e.getStkId()) && e.getSubjectlevel() == 4 && e.getSubjectAttrId().toUpperCase().indexOf("INVESTAPPRECIATION") == -1) .collect(Collectors.toMap(e -> e.getSubjectId(), e -> e)); Alibaba: Map<String, BSide_EV_Subject> filterSubjects = allSubjects.values().stream() .filter(e -> !ParaChecker.isNull(e.getStkId()) && e.getSubjectlevel() == 4 && e.getSubjectAttrId().toUpperCase().indexOf("INVESTAPPRECIATION") == -1) .collect(Collectors.toMap(e -> e.getSubjectId(), e -> e));
-
邏輯判斷:
Google: if (MathUtil.round(inner.getStkValue(), 4) != MathUtil.round(outer.getStkValue(), 4) || (!subject.equals("1109") && MathUtil.round(inner.getPostCostAmt(), 4) != MathUtil.round(outer.getPostCostAmt(), 4))) {} Alibaba: if (MathUtil.round(inner.getStkValue(), 4) != MathUtil.round(outer.getStkValue(), 4) || (!subject.equals("1109") && MathUtil.round(inner.getPostCostAmt(), 4) != MathUtil .round(outer.getPostCostAmt(), 4))) {} Google: if (subject.length() > 8 && !subject.startsWith("1202") && !subject.startsWith("2202") && !onlyCheckStkValue(inner) && MathUtil.round(inner.getPostQtyF(), 4) != MathUtil.round(outer.getPostQtyF(), 4)) {} Alibaba: if (subject.length() > 8 && !subject.startsWith("1202") && !subject.startsWith("2202") && !onlyCheckStkValue(inner) && MathUtil.round(inner.getPostQtyF(), 4) != MathUtil.round(outer.getPostQtyF(), 4)) {}
-
三目運(yùn)算:
Google: EvaluationUtil.recordEVProcess( productNum, tradeDate, isAsset ? EvaluationConst.ASSET_EV_PROCESS : checkValueType == EvaluationConst.CHECKVALUETYPE_1 ? EvaluationConst.EV_PROCESS_REALTIME : EvaluationConst.EV_PROCESS_CHECK, checkValueType == EvaluationConst.CHECKVALUETYPE_1 ? EvaluationConst.EV_Process_realtime_19 : EvaluationConst.EV_Process_check_3, ev_status, beginTime, endTime, diff, flag, evaluationType, null); Alibaba: EvaluationUtil.recordEVProcess(productNum, tradeDate, isAsset ? EvaluationConst.ASSET_EV_PROCESS : checkValueType == EvaluationConst.CHECKVALUETYPE_1 ? EvaluationConst.EV_PROCESS_REALTIME : EvaluationConst.EV_PROCESS_CHECK, checkValueType == EvaluationConst.CHECKVALUETYPE_1 ? EvaluationConst.EV_Process_realtime_19 : EvaluationConst.EV_Process_check_3, ev_status, beginTime, endTime, diff, flag, evaluationType, null);