简体   繁体   English

我应该如何重构这个嵌套的 IF 语句?

[英]How should I refactor this nested IF statement?

I have this function which has more then 3 nested if statement(4 nested If in the function).我有这个 function 有超过 3 个嵌套 if 语句(4 个嵌套 If 在函数中)。 I am trying to solve sonar issue where it says to refactor this code to not nest more than 3 IF/FOR/WHILE/TRY statements .我正在尝试解决声纳问题,它说要refactor this code to not nest more than 3 IF/FOR/WHILE/TRY statements I searched around but couldn't find anything too useful is it possible to reduce?我四处寻找,但找不到任何有用的东西,可以减少吗? - if so, how would I go about doing so? - 如果是这样,我将如何 go 这样做? Any feedback are appreciated任何反馈表示赞赏

Here is the function这是 function

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);

It's hard to reason about your function because it doesn't return anything.很难对您的 function 进行推理,因为它不返回任何内容。 I would have a very hard time tracing the flow of a program that relied on these side-effect only functions like this.我将很难跟踪依赖于这些仅像这样的副作用功能的程序的流程。

But, one thing you could probably do is decompose this function more and start using return values.但是,您可能会做的一件事是进一步分解这个 function 并开始使用返回值。 For instance, split off this piece of code, and return the redirectURL in your new function.例如,拆分这段代码,并在新的 function 中返回 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;

        }

If you need to return multiple items you can either use global variables or return an object and destructure it on the other side.如果您需要返回多个项目,您可以使用全局变量或返回 object 并在另一侧对其进行解构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM