帮助重构这个C#函数

ned*_*sUK 23 c#

我写的函数看起来像这样:

bool IsDry(bool isRaining, bool isWithUmbrella) {
    if (isRaining) {
        if (isWithUmbrella)
            return true;
        else
            return false;
    }
    else
        return true;
}
Run Code Online (Sandbox Code Playgroud)

我需要检查,如果下雨,那么这个人需要带伞以保持干燥(不要笑,这只是一个例子,我们的实际业务规则比这更严重).

我怎么能重构这个,因为现在它看起来很笨拙.

谢谢你的帮助,伙计们!=)

pol*_*nts 69

看起来您正在尝试执行的业务规则是:

P IMPLIES Q
Run Code Online (Sandbox Code Playgroud)

这在逻辑上等同于:

(NOT P) OR Q
Run Code Online (Sandbox Code Playgroud)

因此,您可以简单地写:

bool IsDry(bool isRaining, bool isWithUmbrella) {
    return !isRaining || isWithUmbrella;
}
Run Code Online (Sandbox Code Playgroud)

(不)消极地思考

根据谓词,首先要考虑它的否定也可能更简单.

NOT (P IMPLIES Q)
Run Code Online (Sandbox Code Playgroud)

我们现在替换上面的身份:

NOT ((NOT P) OR Q)
Run Code Online (Sandbox Code Playgroud)

现在我们可以申请德摩根定律:

P AND (NOT Q)
Run Code Online (Sandbox Code Playgroud)

由于这是否定,我们必须否定这一点,以回到正面.双重否定一开始可能会让人感到困惑,但回到这个例子,我们有:

bool IsDry(bool isRaining, bool isWithUmbrella) {
    bool isWet = (isRaining && !isWithUmbrella);
    return !isWet;
}
Run Code Online (Sandbox Code Playgroud)

其他提示

以下是常见boolean表达式重写的一些示例:

BEFORE                                  | AFTER
________________________________________|________________________________________
                                        |
if (condition == true) ...              | if (condition) ...
________________________________________|________________________________________
                                        |
if (condition == false) ...             | if (!condition) ...
________________________________________|________________________________________
                                        |
if (condition) {                        | return condition;
    return true;                        |
} else {                                |
    return false;                       |
}                                       |
________________________________________|________________________________________
                                        |
if (condition1) {                       | return (condition1 && condition2
   if (condition2) {                    |             && condition3);
      if (condition3) {                 |
         return true;                   |
      } else {                          |
         return false;                  |
      }                                 |
   } else {                             |
      return false;                     |
   }                                    |
} else {                                |
   return false;                        |
}                                       |
________________________________________|________________________________________
                                        |
return (condition1 && !condition2) ||   | return condition1 != condition2;
   (condition2 && !condition1);         | // or  condition1 ^ condition2;
Run Code Online (Sandbox Code Playgroud)

请注意,^C#中的预定义是异或运算符,即使对于整数类型(即它不是取幂运算符).预定义&&||条件逻辑运算符,执行"短路"评估.

也可以看看

  • +1.为了补充一点,`!(isRaining &&!isWithUmbrella)`等同于DeMorgan的`(!isRaining || isWithUmbrella)`. (3认同)

Nul*_*ion 11

bool IsDry(bool isRaining, bool isWithUmbrella) 
{
    return (!isRaining || isWithUmbrella);
}
Run Code Online (Sandbox Code Playgroud)


Mit*_*eat 8

bool IsDry(bool isRaining, bool isWithUmbrella)
{
    return !isRaining || isWithUmbrella;
}
Run Code Online (Sandbox Code Playgroud)


Dou*_*las 5

这是一个真值表:

isRaining    isWithUmbrella    isWet    isDry 
true         true              false    true
true         false             true     false
false        true              false    true
false        false             false    true
Run Code Online (Sandbox Code Playgroud)

一个答案可能是:

var isWet = isRaining && !isWithUmbrella;
return !isWet;
Run Code Online (Sandbox Code Playgroud)


Fer*_*cio 5

我通常采用的方法是不断改进.例如,首先消除内部的if-else:

bool IsDry(bool isRaining, bool isWithUmbrella) {
    if (isRaining)
        return isWithUmbrella;
    else
        return true;
}
Run Code Online (Sandbox Code Playgroud)

然后崩溃if

bool IsDry(bool isRaining, bool isWithUmbrella) {
    return isRaining ? isWithUmbrella : true;
}
Run Code Online (Sandbox Code Playgroud)