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

时间:2019-10-17 02:54:50

标签: javascript sonarqube

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

这是功能

function redirectNonSupportedUsers(country) {

    var countryCode = getCodeByCountry(country);

    if (typeof countryCode !== 'undefined' && !isCountrySupported(countryCode)) ---> Nesting 1{

        var redirectURL = $('.menu-select-country li[data-country="' + countryCode + '"] a').attr('href');
        var newQuery = '';

        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://.com';

            newQuery = '?src=' + countryCode;

        }
        changeCountryCookie(countryCode);
        window.location.replace(redirectURL + newQuery);

    } else if (typeof countryCode === 'undefined') {
        noCountryInformation();

    } else {
        handleCountry();

    }
}
return {

    getGeoLocation: function(country) {

      checkCountryCookie(country);

      handleCountry();

    },

    changeCountryCookie: function(country) {

      changeCountryCookie(country);

    },

    getGeoLocationV2: function() {

      var theSrc = $.url().param('src');

      var countryCookie = $.cookie('trp-country');

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

        var countryCheckTheSrc = getCountryByCode(theSrc.toUpperCase());

      }
      if (typeof theSrc !== 'undefined' && typeof countryCheckTheSrc !== 'undefined') {

        if (countryCookie !== countryCheckTheSrc) {

          $.cookie('trp-country', countryCheckTheSrc, { domain: cookieDomain, path: '/', expires: 365 });

        }
        checkCountryCookie(countryCheckTheSrc);

      } else {
        if (typeof countryCookie !== 'undefined') {

          checkCountryCookie(countryCookie);
        } else {

          getCountry();

        }}}};

})(jQuery);

1 个答案:

答案 0 :(得分: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;

        }

如果需要返回多个项目,则可以使用全局变量,也可以返回一个对象并在另一侧对其进行销毁。