【问题标题】:Refactor this function to reduce its Cognitive Complexity from 17 to the 15 allowed重构此函数以将其认知复杂度从 17 降低到允许的 15
【发布时间】:2021-09-11 13:38:00
【问题描述】:

我在 Sonarqube 上遇到了这个错误,如果有的话,请帮我解决这个问题

if (cameFrom === 'textbox') {
    let exceedMinPrice = false;
    let exceedMaxPrice = false;
    let priceRangeError = false;
    if (low < minValue) {
      exceedMinPrice = true;
    }
    if (high > maxValue) {
      exceedMaxPrice = true;
    }
    if (high < low) {
      priceRangeError = true;
    }

【问题讨论】:

  • 也许...let exceedMinPrice = low &lt; minValue;等?
  • @JaromandaX 正要说同样的话,这很容易获胜。
  • @JaromandaX,现在知道了。谢谢
  • 没有问题 - 最终你也会弄清楚如何嵌入图像:p
  • 请在发布问题之前使用预览并检查所有内容是否“可见”并且看起来应该看起来像

标签: javascript reactjs react-native sonarqube


【解决方案1】:

我不确定您使用的工具试图准确表明什么,但一种可能的降低认知复杂性的解决方案如下:

const exceedMinPrice = (low < minValue);
const exceedMaxPrice = (high > maxValue);
const priceRangeError = (high < low);

【讨论】:

  • 是的@richard,现在已修复。谢谢
猜你喜欢
  • 2020-11-02
  • 2021-07-01
  • 2018-06-08
  • 2021-03-14
  • 2021-10-31
  • 1970-01-01
  • 2019-03-08
  • 1970-01-01
  • 2018-09-19
相关资源
最近更新 更多