SonarQube报错:需重构JS函数将认知复杂度从18降至15
问题
SonarQube检测报错:
30:9 error Refactor this function to reduce its Cognitive Complexity from 18 to the 15 allowed
尝试用if-else if重构条件后,报错消失但代码无法正常运行,请求帮忙重构以下代码:
workItemDetails.value.map((e: any) => { const existingData = touchPointRequests.filter((t) => +t.ticket_no === e.id); if (existingData.length && e.fields['System.State']) { this.updateBrandWebsiteState(existingData, e.fields['System.State']); } const existingGoLiveDate = new Date(new Date(existingData[0].go_live_date).toDateString()); const newGoLiveDate = new Date(new Date(e.fields['Custom.GoLiveDate']).toDateString()); const updateData: any = {}; if ( existingData.length && e.fields['Custom.GoLiveDate'] && newGoLiveDate.getTime() !== existingGoLiveDate.getTime() ) { const zonedDate = utcToZonedTime(new Date(e.fields['Custom.GoLiveDate']), 'Europe/Berlin'); updateData.goLiveDate = zonedDate; } if ( existingData.length && e.fields['Microsoft.VSTS.Common.Priority'] && AzurePriority[e.fields['Microsoft.VSTS.Common.Priority']] !== existingData[0].priority ) { updateData.priority = AzurePriority[e.fields['Microsoft.VSTS.Common.Priority']]; } if (Object.keys(updateData).length) { this.touchPointRequestRepository.UpdateTouchPointRequestById(existingData[0].tpr_id, updateData); } if ( existingData.length && e.fields['Custom.PreviewLinks'] != undefined && existingData[0].preview_link !== e.fields['Custom.PreviewLinks'] ) { this.brandWebsiteRequestRepository.UpdateBrandWebsiteRequestByTPId(existingData[0].tpr_id, { previewLink: e.fields['Custom.PreviewLinks'], }); this.sendPreviewLinkUpdateMail(existingData); } if ( existingData.length && e.fields['System.State'] === 'In Review' && e.fields['Custom.PreviewLinks'] != undefined && existingData[0].feedback_and_comments !== e.fields['Custom.PreviewLinks'] ) { this.brandWebsiteRequestRepository.UpdateBrandWebsiteRequestByTPId(existingData[0].tpr_id, { feedbackAndComments: e.fields['Custom.PreviewLinks'], }); } });
重构方案
以下重构后的代码既满足SonarQube的认知复杂度要求,又保证原有功能完整:
workItemDetails.value.map((e: any) => { // 直接获取匹配的第一个数据项,原逻辑仅用到数组第一个元素 const existingData = touchPointRequests.find((t) => +t.ticket_no === e.id); // 无匹配数据直接终止,消除后续所有重复的长度判断 if (!existingData) return; // 提取常用字段为变量,减少重复的对象属性访问 const systemState = e.fields['System.State']; const goLiveDate = e.fields['Custom.GoLiveDate']; const priority = e.fields['Microsoft.VSTS.Common.Priority']; const previewLinks = e.fields['Custom.PreviewLinks']; // 处理状态更新 if (systemState) { this.updateBrandWebsiteState([existingData], systemState); } // 构建TPR更新数据 const updateData: any = {}; // 处理GoLive日期更新 if (goLiveDate) { const existingGoLiveDate = new Date(new Date(existingData.go_live_date).toDateString()); const newGoLiveDate = new Date(new Date(goLiveDate).toDateString()); if (newGoLiveDate.getTime() !== existingGoLiveDate.getTime()) { const zonedDate = utcToZonedTime(new Date(goLiveDate), 'Europe/Berlin'); updateData.goLiveDate = zonedDate; } } // 处理优先级更新 if (priority) { const mappedPriority = AzurePriority[priority]; if (mappedPriority !== existingData.priority) { updateData.priority = mappedPriority; } } // 执行TPR更新(仅当有需要更新的字段时) if (Object.keys(updateData).length) { this.touchPointRequestRepository.UpdateTouchPointRequestById(existingData.tpr_id, updateData); } // 处理预览链接更新 if (previewLinks !== undefined && existingData.preview_link !== previewLinks) { this.brandWebsiteRequestRepository.UpdateBrandWebsiteRequestByTPId(existingData.tpr_id, { previewLink: previewLinks, }); this.sendPreviewLinkUpdateMail([existingData]); } // 处理反馈评论更新(仅状态为In Review时) if (systemState === 'In Review' && previewLinks !== undefined && existingData.feedback_and_comments !== previewLinks) { this.brandWebsiteRequestRepository.UpdateBrandWebsiteRequestByTPId(existingData.tpr_id, { feedbackAndComments: previewLinks, }); } });
重构说明
- 简化数据获取逻辑:将
filter改为find直接获取匹配项,同时提前判断无数据则终止,消除后续所有existingData.length的重复判断;注意原函数updateBrandWebsiteState和sendPreviewLinkUpdateMail接收数组参数,因此调用时需将单个对象包装为数组[existingData],保证参数类型一致。 - 提取重复字段为变量:把多次访问的
e.fields子字段提取为独立变量,提升代码可读性,减少冗余代码。 - 扁平化条件判断:将原有的多层嵌套条件拆分为扁平化逻辑块,每个块仅负责单一功能,降低主函数的分支嵌套层级。
- 拆分独立功能模块:把状态更新、日期处理、优先级更新等逻辑拆分为独立模块,让代码结构更清晰,认知复杂度自然降低。
这些改动完全保留原有功能,同时将认知复杂度控制在15以下,符合SonarQube的检测要求。
内容的提问来源于stack exchange,提问作者userrj_vj_051620
相关产品推荐
相关产品推荐

