IDE 配置模板文件说明:
文档:java代码格式化模板(阿里代码规范)
地址:https://www.jianshu.com/p/9befe7710176
基于项目需要,调研规范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 模板文件
安装文档:https://www.jianshu.com/p/9befe7710176
优缺点对比
配置:
Google: 缩进、换行之类目前全是代码里固定的,不提供配置文件,自定义支持不友好。不过目前已经很好看了
Alibaba: 基于 eclipse-codestyle.xml ,配置较灵活,同时也很麻烦
具体代码对比示例(自行决定使用哪一个)
-
行开始缩进 Google 2 Alibaba 4
-
换行缩进 都是4
-
单行块注释
Google: /** 外部估值数据 */ Alibaba: /** * 外部估值数据 */
-
换行逻辑:
Google 优先 "=" 后换行,"." "(" 后换行 ,优先级从左往右:= . (
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"} };
-
参数换行:Google 在 "(" 后换行,如果1行不足,会每个单数独占一行 (方法定义和调用一致)
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表达式:
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(