如何降低 Javascript 中 if else 语句的复杂性?
How to reduce the complexity of this if else statements in Javascript?
我有以下场景,其中有多个 if else 条件。
下面代码的圈复杂度显示为7。
是否有更好的方法来使用 Javascript 编写以下代码片段以降低代码的复杂性?
function setTime() {
var currentTime = "3/4/2020, 2:53:42 PM"
var selectedTime = "3/5/2020, 2:53:42 PM"
if( Date.parse(currentTime) < Date.parse(selectedTime)) {
callThisMethod('Current time less than selected time');
} else if (Date.parse(currentTime) > Date.parse(selectedTime)) {
callThisMethod('Current time Greater than selected time');
} else {
callThisMethod('Current time is equal to selected time');
}
}
function callThisMethod(message) {
console.log(message);
}
setTime();
可能的选项之一:
const currentTime = new Date("3/4/2020, 2:53:42 PM"),
selectedTime = new Date("3/5/2020, 2:53:42 PM")
callThisMethod(`Current time is ${currentTime < selectedTime ? 'less than' : currentTime > selectedTime ? 'greater than' : 'equal to'} selected time`)
您可以先获取一个 ISO 日期,然后比较这个日期。
function setTime() {
var currentTime = "3/4/2020, 2:53:42 PM",
selectedTime = "3/5/2020, 2:53:42 PM",
current = new Date(currentTime).toISOString(),
selected = new Date(selectedTime).toISOString();
if (current < selected) {
callThisMethod('Current time less than selected time');
} else if (current > selected) {
callThisMethod('Current time Greater than selected time');
} else {
callThisMethod('Current time is equal to selected time');
}
}
function callThisMethod(message) {
console.log(message);
}
setTime();
function setTime() {
var currentTime = "3/4/2020, 2:53:42 PM";
var selectedTime = "3/5/2020, 2:53:42 PM";
let a = new Date(currentTime).getTime();
let b = new Date(selectedTime).getTime();
let str = ['less than','is equal','greater than'];
let n = (a-b)/Math.abs(a-b) || 0;
callThisMethod(`Current time ${str[n]} selected time`);
}
function callThisMethod(message) {
console.log(message);
}
setTime();
根据https://jshint.com/,这个函数的圈复杂度数是2。
也许你应该写 "to reduce code" 或 "to make nicer code"。这个 if 或 else if 语句并不复杂。
实际上,你为什么不 console.log 来自 if/else 的消息,如果你避免调用另一个函数?
function setTime() {
var currentTime = "3/4/2020, 2:53:42 PM",
selectedTime = "3/5/2020, 2:53:42 PM",
current = new Date(currentTime).toISOString(),
selected = new Date(selectedTime).toISOString();
if (current < selected) {
console.log('Current time less than selected time');
} else if (current > selected) {
console.log('Current time Greater than selected time')
} else {
console.log('Current time is equal to selected time');
}
}
setTime();
这种方法可能有几种可能的答案。
您可以做的一件事是不对条件进行评估。因此,不是在条件表达式中解析日期,而是在比较之前解析它。之后,您可以将您认为可能经常出现的最常见条件作为第一个条件,并使用二进制搜索方法打破其他条件。考虑以下代码片段:
function setTime() {
var currentTime = Date.parse("3/4/2020, 2:53:42 PM")
var selectedTime = Date.parse("3/5/2020, 2:53:42 PM")
if(currentTime < selectedTime) {
callThisMethod('Current time less than selected time');
} else {
if (currentTime > selectedTime) {
callThisMethod('Current time Greater than selected time');
} else {
callThisMethod('Current time is equal to selected time');
}
}
}
function callThisMethod(message) {
console.log(message);
}
setTime();
注意如何使用拆分技术将条件分解为多个部分。
或者您可以使用 switch case 而不是条件语句。与 if-else 条件语句相比,switch case 被发现在后续条件上花费的增量成本更少。
考虑以下代码段:
function setTime() {
var currentTime = Date.parse("3/4/2020, 2:53:42 PM")
var selectedTime = Date.parse("3/5/2020, 2:53:42 PM")
switch(true){
case (currentTime > selectedTime):
callThisMethod('Current time Greater than selected time');
break;
case (currentTime < selectedTime):
callThisMethod('Current time less than selected time')
break;
default:
callThisMethod('Current time is equal to selected time')
}
}
function callThisMethod(message) {
console.log(message);
}
setTime();
我有以下场景,其中有多个 if else 条件。
下面代码的圈复杂度显示为7。
是否有更好的方法来使用 Javascript 编写以下代码片段以降低代码的复杂性?
function setTime() {
var currentTime = "3/4/2020, 2:53:42 PM"
var selectedTime = "3/5/2020, 2:53:42 PM"
if( Date.parse(currentTime) < Date.parse(selectedTime)) {
callThisMethod('Current time less than selected time');
} else if (Date.parse(currentTime) > Date.parse(selectedTime)) {
callThisMethod('Current time Greater than selected time');
} else {
callThisMethod('Current time is equal to selected time');
}
}
function callThisMethod(message) {
console.log(message);
}
setTime();
可能的选项之一:
const currentTime = new Date("3/4/2020, 2:53:42 PM"),
selectedTime = new Date("3/5/2020, 2:53:42 PM")
callThisMethod(`Current time is ${currentTime < selectedTime ? 'less than' : currentTime > selectedTime ? 'greater than' : 'equal to'} selected time`)
您可以先获取一个 ISO 日期,然后比较这个日期。
function setTime() {
var currentTime = "3/4/2020, 2:53:42 PM",
selectedTime = "3/5/2020, 2:53:42 PM",
current = new Date(currentTime).toISOString(),
selected = new Date(selectedTime).toISOString();
if (current < selected) {
callThisMethod('Current time less than selected time');
} else if (current > selected) {
callThisMethod('Current time Greater than selected time');
} else {
callThisMethod('Current time is equal to selected time');
}
}
function callThisMethod(message) {
console.log(message);
}
setTime();
function setTime() {
var currentTime = "3/4/2020, 2:53:42 PM";
var selectedTime = "3/5/2020, 2:53:42 PM";
let a = new Date(currentTime).getTime();
let b = new Date(selectedTime).getTime();
let str = ['less than','is equal','greater than'];
let n = (a-b)/Math.abs(a-b) || 0;
callThisMethod(`Current time ${str[n]} selected time`);
}
function callThisMethod(message) {
console.log(message);
}
setTime();
根据https://jshint.com/,这个函数的圈复杂度数是2。
也许你应该写 "to reduce code" 或 "to make nicer code"。这个 if 或 else if 语句并不复杂。
实际上,你为什么不 console.log 来自 if/else 的消息,如果你避免调用另一个函数?
function setTime() {
var currentTime = "3/4/2020, 2:53:42 PM",
selectedTime = "3/5/2020, 2:53:42 PM",
current = new Date(currentTime).toISOString(),
selected = new Date(selectedTime).toISOString();
if (current < selected) {
console.log('Current time less than selected time');
} else if (current > selected) {
console.log('Current time Greater than selected time')
} else {
console.log('Current time is equal to selected time');
}
}
setTime();
这种方法可能有几种可能的答案。 您可以做的一件事是不对条件进行评估。因此,不是在条件表达式中解析日期,而是在比较之前解析它。之后,您可以将您认为可能经常出现的最常见条件作为第一个条件,并使用二进制搜索方法打破其他条件。考虑以下代码片段:
function setTime() {
var currentTime = Date.parse("3/4/2020, 2:53:42 PM")
var selectedTime = Date.parse("3/5/2020, 2:53:42 PM")
if(currentTime < selectedTime) {
callThisMethod('Current time less than selected time');
} else {
if (currentTime > selectedTime) {
callThisMethod('Current time Greater than selected time');
} else {
callThisMethod('Current time is equal to selected time');
}
}
}
function callThisMethod(message) {
console.log(message);
}
setTime();
注意如何使用拆分技术将条件分解为多个部分。 或者您可以使用 switch case 而不是条件语句。与 if-else 条件语句相比,switch case 被发现在后续条件上花费的增量成本更少。 考虑以下代码段:
function setTime() {
var currentTime = Date.parse("3/4/2020, 2:53:42 PM")
var selectedTime = Date.parse("3/5/2020, 2:53:42 PM")
switch(true){
case (currentTime > selectedTime):
callThisMethod('Current time Greater than selected time');
break;
case (currentTime < selectedTime):
callThisMethod('Current time less than selected time')
break;
default:
callThisMethod('Current time is equal to selected time')
}
}
function callThisMethod(message) {
console.log(message);
}
setTime();