IF 语句 - 如何减少/简化源代码量
IF statements - How to reduce / simplify the amount of source code
我在尝试减少函数的这部分代码时遇到了麻烦:
checkData(day, month, year, area)
{
if(area == "year" && year == this.year)
return true;
if(area == "month" && month == this.div && year == this.year)
return true;
if(area == "day" && day == this.day && month == this.div && year == this.year)
return true;
return false;
}
我如何simplify/reduce这些 IF 子句的源代码量?
您有 3 个 return 相同真值的 if 语句。您可以使用带括号的 ||
orelse 运算符将这些语句组合成一行,如下所示
checkData(day, month, year, area){
if ((area == "year" && year == this.year) || (area == "month" && month == this.div && year == this.year)|| (area == "day" && day == this.day && month == this.div && year == this.year))
return true;
return false;
}
希望这对您有所帮助
EDIT - 后两个 if 语句是否嵌套在第一个语句中?很难判断是否只是格式问题。如果它们是嵌套的,那么您希望 &&
andalso 运算符将第一个 if 语句与 each 组合起来,然后 orelse 将两者组合起来,如下所示:
checkData(day, month, year, area){
if (area == "year" && year == this.year) && (area == "month" && month == this.div && year == this.year)
|| (area == "year" && year == this.year) && (area == "day" && day == this.day && month == this.div && year == this.year)
return true;
return false;
}
您的 if 子句可以重写为:
checkData(day, month, year, area)
{
if(year == this.year) {
if(area == "year") return true;
if(month == this.div) {
if(area == "month") return true;
if(day == this.day) {
if(area == "day") return true;
return false;
}
}
}
}
然后到:
checkData(day, month, year, area)
{
if(year != this.year) return false;
if(area == "year") return true;
if(month != this.div) return false;
if(area == "month") return true;
if(day != this.day) return false;
return area == "day";
}
另一种可能的语法,取决于编程语言(例如 C++):
checkData(day, month, year, area)
{
return
year != this.year ? false :
area == "year" ? true :
month != this.div ? false :
area == "month" ? true :
day != this.day ? false :
area == "day";
}
然后可以写成一行:
checkData(day, month, year, area)
{
return year != this.year ? false : area == "year" ? true : month != this.div ? false : area == "month" ? true : day != this.day ? false : area == "day";
}
有些语言支持这种语法(例如 C#):
checkData(day, month, year, area) => year != this.year ? false : area == "year" ? true : month != this.div ? false : area == "month" ? true : day != this.day ? false : area == "day";
我在尝试减少函数的这部分代码时遇到了麻烦:
checkData(day, month, year, area)
{
if(area == "year" && year == this.year)
return true;
if(area == "month" && month == this.div && year == this.year)
return true;
if(area == "day" && day == this.day && month == this.div && year == this.year)
return true;
return false;
}
我如何simplify/reduce这些 IF 子句的源代码量?
您有 3 个 return 相同真值的 if 语句。您可以使用带括号的 ||
orelse 运算符将这些语句组合成一行,如下所示
checkData(day, month, year, area){
if ((area == "year" && year == this.year) || (area == "month" && month == this.div && year == this.year)|| (area == "day" && day == this.day && month == this.div && year == this.year))
return true;
return false;
}
希望这对您有所帮助
EDIT - 后两个 if 语句是否嵌套在第一个语句中?很难判断是否只是格式问题。如果它们是嵌套的,那么您希望 &&
andalso 运算符将第一个 if 语句与 each 组合起来,然后 orelse 将两者组合起来,如下所示:
checkData(day, month, year, area){
if (area == "year" && year == this.year) && (area == "month" && month == this.div && year == this.year)
|| (area == "year" && year == this.year) && (area == "day" && day == this.day && month == this.div && year == this.year)
return true;
return false;
}
您的 if 子句可以重写为:
checkData(day, month, year, area)
{
if(year == this.year) {
if(area == "year") return true;
if(month == this.div) {
if(area == "month") return true;
if(day == this.day) {
if(area == "day") return true;
return false;
}
}
}
}
然后到:
checkData(day, month, year, area)
{
if(year != this.year) return false;
if(area == "year") return true;
if(month != this.div) return false;
if(area == "month") return true;
if(day != this.day) return false;
return area == "day";
}
另一种可能的语法,取决于编程语言(例如 C++):
checkData(day, month, year, area)
{
return
year != this.year ? false :
area == "year" ? true :
month != this.div ? false :
area == "month" ? true :
day != this.day ? false :
area == "day";
}
然后可以写成一行:
checkData(day, month, year, area)
{
return year != this.year ? false : area == "year" ? true : month != this.div ? false : area == "month" ? true : day != this.day ? false : area == "day";
}
有些语言支持这种语法(例如 C#):
checkData(day, month, year, area) => year != this.year ? false : area == "year" ? true : month != this.div ? false : area == "month" ? true : day != this.day ? false : area == "day";