【问题标题】:How should I refactor this nested IF statement?我应该如何重构这个嵌套的 IF 语句?
【发布时间】:2020-02-13 21:05:30
【问题描述】:

我有这个函数,它有超过 3 个嵌套 if 语句(函数中有 4 个嵌套 If)。我正在尝试解决它对refactor this code to not nest more than 3 IF/FOR/WHILE/TRY statements 的声纳问题。我四处寻找,但找不到任何有用的东西,可以减少吗? - 如果是这样,我将如何去做?任何反馈表示赞赏

这里是函数

function find(animals) {

    var animalsCode = getByanimals(animals);

    if (typeof animalsCode !== 'undefined' && !isanimalsSupported(animalsCode)) ---> Nesting 1{

        var url = $('.menu-select-animals li[data-animals="' + animalsCode + '"] a').attr('href');
        var newQuery = '';

        if (url) {    ---->Nesting 2
            var redirectQuery = '';
            if (window.location.search) {

            //some logic
            }
            if (url.indexOf('?') > -1) {    ---->Nesting 3

               //some logic

                if (typeof join[1] !== 'undefined') { ---->Nesting 4

             //some code
                }
            }

            if (typeof mergedQueries !== 'undefined');
            }
        } else {
          //some code
        }
     else {
        handleanimals();
    }
}
    return {
    handleanimals();
    },

    windows: function() {
      if (typeof theSrc !== 'undefined') {
      // some logic
      }

      if (typeof animalsCheckTheSrc !== 'undefined') {

        if (animalsCookie !== animalsCheckTheSrc) {
        // some logic
        }
        checkanimalsCookie(animalsCheckTheSrc);
      } }}};
})(jQuery);

【问题讨论】:

  • 你能修复你的代码示例吗?看起来它至少缺少一个右括号。
  • 编辑了我的代码。
  • 如果您有有效的代码并正在寻找评论,您可能需要检查codereview.stackexchange.com
  • 此代码不审核,试图解决问题

标签: javascript sonarqube


【解决方案1】:

很难推断您的函数,因为它不返回任何内容。我将很难跟踪依赖于这些仅像这样的副作用函数的程序的流程。

但是,您可能会做的一件事是进一步分解此函数并开始使用返回值。例如,拆分这段代码,并在新函数中返回 redirectURL。

if (redirectURL) {    ---->Nesting 2

            var pageQuery = '';

            var redirectQuery = '';
            if (window.location.search) {

                pageQuery = window.location.search.substr(1).split('&');

            }
            if (redirectURL.indexOf('?') > -1) {    ---->Nesting 3

                var redirectSplit = redirectURL.split('?');

                redirectURL = redirectSplit[0];


                if (typeof redirectSplit[1] !== 'undefined') { ---->Nesting 4

                    redirectQuery = redirectSplit[1].split('&');

                }
            }
            var mergedQueries = com.trp.fai.utility.mergeStringArrays(redirectQuery, pageQuery);



            if (typeof mergedQueries !== 'undefined' && mergedQueries.length > 0) {

                newQuery = '?' + mergedQueries.join('&');

            }
        } else {

            redirectURL = 'http://corporate.troweprice.com';

            newQuery = '?src=' + countryCode;

        }

如果您需要返回多个项目,您可以使用全局变量或返回一个对象并在另一端对其进行解构。

【讨论】:

  • 感谢您的快速解决,它确实会返回,但在其他功能中,如果可以,请在代码中添加更多详细信息
  • 嘿,这对我不起作用,您能否提出一些解决方案,谢谢
  • @Karan 您没有提供太多上下文,您可以编辑您的 OP 以澄清您现在看到的错误吗?
  • 我没有看到任何错误,我已经在我的代码中添加了更多细节,如果你可以看一次
  • @Karan 那个 sonarqube 错误消失了吗?您是否尝试过在 DevTools 中使用断点跟踪您的代码,以查看某个变量是否在某个时间点包含意外值?
猜你喜欢
  • 1970-01-01
  • 2020-01-02
  • 2015-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-01
  • 1970-01-01
相关资源
最近更新 更多