Commit f83d4bf7 by 杨卫

feat:处理数据格式

1 parent d307ba03
...@@ -327,23 +327,23 @@ public class WarningEngineRemoteImpl implements WarningEngineRemote { ...@@ -327,23 +327,23 @@ public class WarningEngineRemoteImpl implements WarningEngineRemote {
metricDto.setUnit(metricDict != null ? metricDict.getUnit() : null); metricDto.setUnit(metricDict != null ? metricDict.getUnit() : null);
BigDecimal nextValue = nextByStore.get(storeId); BigDecimal nextValue = nextByStore.get(storeId);
if (nextValue == null) { boolean hasNextValue = nextValue != null;
nextValue = BigDecimal.ZERO; if (hasNextValue) {
} else {
if ("生态系数".equals(metricName)) { if ("生态系数".equals(metricName)) {
nextValue = applyEcoCoefficient(metricCode, nextValue); nextValue = applyEcoCoefficient(metricCode, nextValue);
hasNextValue = nextValue != null;
} }
if (needPercentScale) { if (hasNextValue && needPercentScale) {
nextValue = nextValue.multiply(new BigDecimal("100")); nextValue = nextValue.multiply(new BigDecimal("100"));
} }
} }
metricDto.setNextValue(nextValue.setScale(2, RoundingMode.HALF_UP)); metricDto.setNextValue(nextValue != null ? nextValue.setScale(2, RoundingMode.HALF_UP) : null);
if (currentValue != null && currentValue.compareTo(BigDecimal.ZERO) != 0) { if (hasNextValue && currentValue != null && currentValue.compareTo(BigDecimal.ZERO) != 0) {
BigDecimal changeRate = nextValue.subtract(currentValue) BigDecimal changeRate = nextValue.subtract(currentValue)
.divide(currentValue, 2, RoundingMode.HALF_UP); .divide(currentValue, 2, RoundingMode.HALF_UP);
metricDto.setChangeRate(changeRate); metricDto.setChangeRate(changeRate);
metricDto.setImproved(changeRate.compareTo(BigDecimal.ZERO) > 0); metricDto.setImproved(nextValue.compareTo(currentValue) > 0);
} }
storeMetricsResult.get(storeId).add(metricDto); storeMetricsResult.get(storeId).add(metricDto);
} }
......
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议,如果必要,给出代码示例(仅限 Top 3)

    1. 潜在的下游调用方空指针风险(契约变更)

    • 问题描述:原逻辑中,当 nextValue 为空时,会默认赋值为 BigDecimal.ZERO。修改后的逻辑将其保留为 null,并通过 metricDto.setNextValue(null) 返回给调用方。
    • 影响分析:当前类 WarningEngineRemoteImpl 是一个 RPC (Dubbo) 服务提供者。如果下游消费方(Consumer)的代码一直依赖 nextValue 不为空的假设(例如直接调用 .doubleValue() 或进行数学运算),此变更将直接导致下游系统出现 NullPointerException。
    • 优化建议:虽然从业务语义上看,将“无数据”表示为 null 比表示为 0 更合理,但作为基础服务,需强烈建议排查所有调用该接口的下游服务,确认其是否能兼容 nextValue 为 null 的情况。如果下游无法兼容,应在 DTO 层面做兼容处理或与调用方同步发布。

    2. 状态变量重复赋值导致可读性下降

    • 问题描述:代码中引入了 hasNextValue 标志位,但在 applyEcoCoefficient 方法调用后,又对 hasNextValue 进行了二次重新赋值(hasNextValue = nextValue != null;)。
    • 影响分析:状态变量在同一代码块中被多次改变,增加了代码的认知负荷,不符合单一职责与防御性编程的最佳实践,容易在后续迭代中引发逻辑遗漏。
    • 优化建议:可以通过提前返回或嵌套判空的方式,减少局部变量的突变。 代码示例:

      BigDecimal nextValue = nextByStore.get(storeId);
      if (nextValue != null) {
        if ("生态系数".equals(metricName)) {
            nextValue = applyEcoCoefficient(metricCode, nextValue);
        }
        // 重新判空,因为 applyEcoCoefficient 可能返回 null
        if (nextValue != null) {
            if (needPercentScale) {
                nextValue = nextValue.multiply(new BigDecimal("100"));
            }
            metricDto.setNextValue(nextValue.setScale(2, RoundingMode.HALF_UP));
      
            if (currentValue != null && currentValue.compareTo(BigDecimal.ZERO) != 0) {
                BigDecimal changeRate = nextValue.subtract(currentValue)
                        .divide(currentValue, 2, RoundingMode.HALF_UP);
                metricDto.setChangeRate(changeRate);
                // 优化:直接使用原值比较,比使用舍入后的 changeRate 更精确
                metricDto.setImproved(nextValue.compareTo(currentValue) > 0); 
            }
        }
      } else {
        metricDto.setNextValue(null);
      }
      

    3. 提交信息(Commit Message)表述过于宽泛

    • 问题描述:提交信息为 feat:处理数据格式。
    • 影响分析:该变更实际上修改了核心的业务逻辑(将默认值 0 改为 null,且优化了 improved 字段的计算规则)。宽泛的提交信息掩盖了业务逻辑的变更,导致后续排查线上问题或进行代码回溯时,无法快速理解修改的真实意图。
    • 优化建议:提交信息应遵循规范,准确描述“做了什么”和“为什么做”。建议修改为类似:fix: 调整指标计算逻辑,将空值保留为null而非0以区分无数据与0值,并提升改善率(improved)判断的精度。

    二、评分明细

    • 功能实现的正确性与健壮性(35/40分):
      • 理由:逻辑内部的空指针防御做得很好(如 hasNextValue 的联动判断)。将 improved 的判断从依赖舍入后的 changeRate 改为直接比对 nextValue.compareTo(currentValue),有效避免了精度丢失导致的误判,这是一个很好的隐性修复。扣分点在于 RPC 接口返回值语义变更(0变null)带来的潜在边界崩溃风险。
    • 安全性与潜在风险(30/30分):
      • 理由:本次变更仅涉及内存中的数值计算与 DTO 赋值,未涉及 SQL 操作、外部输入执行等,不存在 SQL 注入、越权等安全风险。
    • 最佳实践与可维护性(16/20分):
      • 理由:代码整体符合 Java 编码规范,但局部状态变量 hasNextValue 的反复覆盖赋值不够优雅,增加了阅读理解成本。
    • 性能与资源利用(5/5分):
      • 理由:仅为基础的 BigDecimal 运算和对象赋值,无循环内的昂贵操作,无性能瓶颈。
    • 提交信息质量(2/5分):
      • 理由:提交信息过于简略,未能体现出业务逻辑(0值处理、精度优化)的真实变更意图,缺乏可追溯性。

    总分:88分

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!