英文:
How to refactor nested if condition in a function to reduce cognitive complexity in sonar?
问题
在我的组件中,我编写了一个用于条件检查以禁用/启用按钮的函数。有多个嵌套的条件检查。在运行时,我收到了Sonar中的“减少认知复杂性”错误,而且这是一个关键问题。
这是我的函数
const isButtonSubmit = computed(() => {
let disableButton = false;
if (formInput.value.radio3 === 'yes') {
if (formInput.value.input1 === '') {
disableButton = true;
}
}
if (formInput.value.radio4 === 'yes') {
if (formInput.value.input2 === '') {
disableButton = true;
}
}
if (formInput.value.radio4 === 'no') {
if (formInput.value.input6 === '') {
disableButton = true;
}
}
if (formInput.value.radio5 === 'no') {
if (formInput.value.input3 === '') {
disableButton = true;
}
}
if (formInput.value.radio6 === 'yes') {
if (formInput.value.input4 === '') {
disableButton = true;
}
}
if (formInput.value.radio6 === 'no') {
if (formInput.value.input5 === '') {
disableButton = true;
}
}
if (formInput.value.radio7 === 'no') {
if (formInput.value.input7 === '') {
disableButton = true;
}
}
if (formInput.value.radio8 === 'no') {
if (formInput.value.input8 === '') {
disableButton = true;
}
}
if (formInput.value.radio9 === 'no') {
if (formInput.value.input9 === '') {
disableButton = true;
}
}
return disableButton;
});
我因为这些条件检查而收到了与认知复杂性相关的警告。请告诉我如何解决这个问题?
英文:
In my component, i have written one function for conditional check to disable/enable a button. There are multiple nested conditional check. While running, i am getting 'Reduce coginitive complexity' error in sonar and it is critical.
Here is my function
const isButtonSubmit = computed(() => {
let disableButton = false;
if (formInput.value.radio3 === 'yes') {
if (formInput.value.input1 === '') {
disableButton = true;
}
}
if (formInput.value.radio4 === 'yes') {
if (formInput.value.input2 === '') {
disableButton = true;
}
}
if (formInput.value.radio4 === 'no') {
if (formInput.value.input6 === '') {
disableButton = true;
}
}
if (formInput.value.radio5 === 'no') {
if (formInput.value.input3 === '') {
disableButton = true;
}
}
if (formInput.value.radio6 === 'yes') {
if (formInput.value.input4 === '') {
disableButton = true;
}
}
if (formInput.value.radio6 === 'no') {
if (formInput.value.input5 === '') {
disableButton = true;
}
}
if (formInput.value.radio7 === 'no') {
if (formInput.value.input7 === '') {
disableButton = true;
}
}
if (formInput.value.radio8 === 'no') {
if (formInput.value.input8 === '') {
disableButton = true;
}
}
if (formInput.value.radio9 === 'no') {
if (formInput.value.input9 === '') {
disableButton = true;
}
}
return disableButton;
});
I am getting the warning related to cognitive complexity because of these conditional checks. Please let me know how to resolve this?
答案1
得分: 1
在你的情况下,这可以是这样的:
const disableStates = [
{radio: 'radio3', value: 'yes', input: 'input1'},
{radio: 'radio4', value: 'yes', input: 'input2'},
// ...
]
const isButtonSubmit = computed(() => {
const i = formInput.value
return disableStates.some(({radio, value, input}) => i[radio] === value && i[input] === '')
}
希望这对你有所帮助。
英文:
The approach to issues like this is always the same, you have to extract the data from the structure and then find a way to apply it efficiently. You should end up with more maintainable and compact code.
In your case, this could be something like this:
const disableStates = [
{radio: 'radio3', value: 'yes', input: 'input1'},
{radio: 'radio4', value: 'yes', input: 'input2'},
...
]
const isButtonSubmit = computed(() => {
const i = formInput.value
return disableStates.some(({radio, value, input}) => i[radio] === value && i[input] === '')
}
答案2
得分: 0
OP 可以使用对象字面量作为配置,该对象字面量映射了所有相关数据,定义了计算按钮值为 disabled 的情况。下面提供的示例代码通过迭代所有涉及的元素键的数组来执行此操作,对于每个键,都检索其相关的表单输入值,并从后者获取用于比较值的配置。提供的数据结构在元素值方面尽可能通用,因此仍然可以根据需要针对单个元素值进行更改...
const submitConfig = {
radio3: {
'yes': { comparisonKey: 'input1', comparisonValue: '' },
},
radio4: {
'yes': { comparisonKey: 'input2', comparisonValue: '' },
'no': { comparisonKey: 'input6', comparisonValue: '' },
},
radio5: {
'no': { comparisonKey: 'input3', comparisonValue: '' },
},
radio6: {
'yes': { comparisonKey: 'input4', comparisonValue: '' },
'no': { comparisonKey: 'input5', comparisonValue: '' },
},
radio7: {
'no': { comparisonKey: 'input7', comparisonValue: '' },
},
radio8: {
'no': { comparisonKey: 'input8', comparisonValue: '' },
},
radio9: {
'no': { comparisonKey: 'input9', comparisonValue: '' },
},
};
const isButtonSubmit = computed(() => {
let disableButton = false;
// 元素键的优先级很重要
// [ 'radio3', 'radio4', 'radio5', 'radio6', 'radio7', 'radio8', 'radio9' ]
// .forEach( ... )
// 或者 ...
Object
.keys(submitConfig)
// 只有在首次遇到禁用按钮值并且表示停止任何进一步迭代时,才应选择`some`。
// (但在 OP 提供的代码中没有指示。)
.forEach(elementKey => {
const inputValues = formInput.value;
const elementValue = inputValues[elementKey];
const elementConfig = submitConfig[elementKey]?.[elementValue] ?? null;
if (elementConfig !== null) {
const { comparisonKey, comparisonValue } = elementConfig;
disableButton = (inputValues[comparisonKey] === comparisonValue);
}
});
return disableButton;
});
如果您需要进一步的帮助或有其他问题,请随时提出。
英文:
The OP could utilize an object literal as configuration which does map all the related data which define the computed button value as disabled. The next provided example code does so by iterating the array of all involved element keys, where for each key one retrieves its related form input value and from the latter the effected configuration for comparing values. The provided data structure is as generic as possible from the element-value side thus still open to changes which target element values individually ...
const submitConfig = {
radio3: {
'yes': { comparisonKey: 'input1', comparisonValue: '' },
},
radio4: {
'yes': { comparisonKey: 'input2', comparisonValue: '' },
'no': { comparisonKey: 'input6', comparisonValue: '' },
},
radio5: {
'no': { comparisonKey: 'input3', comparisonValue: '' },
},
radio6: {
'yes': { comparisonKey: 'input4', comparisonValue: '' },
'no': { comparisonKey: 'input5', comparisonValue: '' },
}
radio7: {
'no': { comparisonKey: 'input7', comparisonValue: '' },
},
radio8: {
'no': { comparisonKey: 'input8', comparisonValue: '' },
},
radio9: {
'no': { comparisonKey: 'input9', comparisonValue: '' },
},
};
const isButtonSubmit = computed(() => {
let disableButton = false;
// the precedence of element keys matters
// ['radio3', 'radio4', 'radio5', 'radio6', 'radio7', 'radio8', 'radio9']
// .forEach( ... )
// or ...
Object
.keys(submitConfig)
// `some` should be chosen only in case the first encounter of an
// disabled button value also means stopping any further iterations.
// (but there was no indication within the OP'S provided code.)
.forEach(elementKey => {
const inputValues = formInput.value;
const elementValue = inputValues[elementKey];
const elementConfig = submitConfig[elementKey]?.[elementValue] ?? null;
if (elementConfig !== null) {
const { comparisonKey, comparisonValue } = elementConfig;
disableButton = (inputValues[comparisonKey] === comparisonValue);
}
});
return disableButton;
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论