【发布时间】: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