Commit df6c22b2 by 崔彦峰

fix(goods): 修复商品列表提交服务中的空指针异常

- 添加对施工项目分页结果的空值检查
- 验证分页结果列表是否为空避免索引越界
- 确保在访问列表元素前进行安全检查
- 防止因远程调用返回空数据导致的服务中断
1 parent d1dcb617
......@@ -3015,11 +3015,13 @@ public class GoodsListingSubmitService {
pagination.setPageSize(1);
RpcResponse<RpcPagination<ConstructionServiceItmes>> constructionItemPage = constructionRemote.findConstructionItemPage(pagination);
if (constructionItemPage.isSuccess()) {
ConstructionServiceItmes constructionServiceItmes = constructionItemPage.getResult().getList().get(0);
RpcResponse<ProductCostCalculateDTO> productCostCalculateDTORpcResponse = merchantGoodsInfoRemote.calculateProductCost(upgradeGoodsId, constructionServiceItmes.getId());
if (productCostCalculateDTORpcResponse.isSuccess()) {
if (productCostCalculateDTORpcResponse.getResult() != null) {
upgradeProductCost = productCostCalculateDTORpcResponse.getResult().getUpgradeCost();
if (constructionItemPage.getResult() != null && CollectionUtils.isNotEmpty(constructionItemPage.getResult().getList())) {
ConstructionServiceItmes constructionServiceItmes = constructionItemPage.getResult().getList().get(0);
RpcResponse<ProductCostCalculateDTO> productCostCalculateDTORpcResponse = merchantGoodsInfoRemote.calculateProductCost(upgradeGoodsId, constructionServiceItmes.getId());
if (productCostCalculateDTORpcResponse.isSuccess()) {
if (productCostCalculateDTORpcResponse.getResult() != null) {
upgradeProductCost = productCostCalculateDTORpcResponse.getResult().getUpgradeCost();
}
}
}
}
......
  • @wangxw1
    commented
    Master

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

    1. 代码结构与可读性(条件嵌套过深)

    • 问题描述:本次修改为了增加健壮性,引入了新的嵌套 if 语句,导致代码缩进层次加深(即“箭头型代码”),包括内部的 productCostCalculateDTORpcResponse 判断也是嵌套形式。
    • 影响分析:过深的嵌套结构会增加代码的阅读和理解成本,降低可维护性,不利于后续功能的扩展。
    • 优化建议:对于没有 else 分支的连续条件判断,建议将其合并为一个 if 语句,使代码更加扁平化。

    代码示例:

    // 优化后的代码结构
    if (constructionItemPage.isSuccess() 
            && constructionItemPage.getResult() != null 
            && CollectionUtils.isNotEmpty(constructionItemPage.getResult().getList())) {
    
        ConstructionServiceItmes constructionServiceItmes = constructionItemPage.getResult().getList().get(0);
        RpcResponse<ProductCostCalculateDTO> costResponse = merchantGoodsInfoRemote.calculateProductCost(upgradeGoodsId, constructionServiceItmes.getId());
    
        // 合并内部的 if 条件
        if (costResponse.isSuccess() && costResponse.getResult() != null) {
            upgradeProductCost = costResponse.getResult().getUpgradeCost();
        }
    }
    

    2. 命名规范(历史遗留拼写错误)

    • 问题描述:虽然不是本次变更引入的,但实体类名及变量名 ConstructionServiceItmes 中存在明显的单词拼写错误,Itmes 应为 Items。
    • 影响分析:拼写错误会给后续接手维护该模块的开发人员带来困扰,降低代码的专业度,不符合主流工程最佳实践。
    • 优化建议:建议在合适的时机(或单独拉一个重构分支),利用 IDE 的 Rename 功能全局修复该拼写错误,修改为 ConstructionServiceItems。

    二、评分明细

    • 功能实现的正确性与健壮性(40/40分): 理由:本次修改非常精准地修复了原代码中潜在的 NullPointerException 以及 IndexOutOfBoundsException,增加了严谨的边界检查,逻辑完全正确,健壮性得到明显提升。

    • 安全性与潜在风险(30/30分): 理由:未引入新的安全隐患,反而通过防止异常抛出,降低了服务因未捕获异常而中断的风险,提升了系统的稳定性。

    • 最佳实践与可维护性(17/20分): 理由:扣除3分。主要是因为新增的逻辑采用了深层嵌套的 if 语句,降低了代码可读性;此外代码中暴露出历史遗留的命名拼写错误。

    • 性能与资源利用(5/5分): 理由:仅增加了必要的内存引用判断与集合大小判断,属于 O(1) 操作,无任何性能瓶颈或资源浪费。

    • 提交信息质量(5/5分): 理由:Commit 信息采用了标准的 Conventional Commits 规范(fix(goods): ...),条理清晰,详细列出了修改原因和具体措施,可追溯性极佳。

    总分:97分

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