JAVA代码格式 Google-java-format VS AlibabaP3C

IDE 配置模板文件说明:color{red}{新增基于Google格式的xml配置文件}

文档: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 ,配置较灵活,同时也很麻烦

具体代码对比示例(自行决定使用哪一个)

  1. 行开始缩进 Google 2 Alibaba 4

  2. 换行缩进 都是4

  3. 单行块注释

    Google:
      /** 外部估值数据 */
    Alibaba: 
        /**
         * 外部估值数据
         */
    
  4. 换行逻辑:
    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"));
    
  5. 代码块换行: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"}
        };
    
  6. 参数换行: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);
    
  7. 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(), 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));
    
  8. 逻辑判断:

    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)) {}
    
  9. 三目运算:

    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);
    
 

链接:https://www.jianshu.com/p/17a145a76b07
来源:简书

原文地址:https://www.cnblogs.com/shoshana-kong/p/14981129.html