构造条件逻辑的最佳方法是什么? [英] What is the best way to structure conditional logic?

查看:85
本文介绍了构造条件逻辑的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,有很多不同的方法来构造条件逻辑。据我所知,只要我们设置错误以结束脚本(或者您可以想象相同的示例,但是函数返回),那么以下示例是相等的:

It occurs to me that there are number of different ways to structure conditional logic. As far as I can see, as long as we set errors to end the script (or you can imagine the same examples but with a return in a function), then the following examples are equal:

示例1

if($condition1) {
    trigger_error("The script is now terminated");
    }

if($condition2) {
    trigger_error("The script is now terminated");
    }

echo "If either condition was true, we won't see this printed";

示例2

if(!$condition1) {
    if(!$condition2) {
        echo "If either condition was true, we won't see this printed";
        }
    else {
        trigger_error("The script is now terminated");
        }
    }
else {
    trigger_error("The script is now terminated");
    }

示例3

if($condition1) {
    trigger_error("The script is now terminated");
    }
else {
    if($condition2) {
        trigger_error("The script is now terminated");
        }
    else {
        echo "If either condition was true, we won't see this printed";
        }
    }

示例4 -改编自 Fraser的答案

function test($condition) { 
    if($condition) {
        trigger_error("The script is now terminated");
        }   
    }

test($condition1);

test($condition2);

echo "If either condition was true, we won't see this printed";

我个人倾向于编写示例1中的代码。这是因为我认为通过检查以这种方式结束脚本(或函数)的条件,我可以清楚地定义脚本执行和不执行的内容,即条件执行之前的所有内容和该行之后的所有内容都没有执行。这意味着当我在第147行出现错误时,我立即知道发生了什么事,这有助于我更快地找到错误。此外,如果我突然意识到需要在$ condition1之前测试$ condition2,则可以通过简单的复制粘贴进行更改。

Personally, I lean towards writing code as in Example 1. This is because I feel that by checking for conditions that end the script (or function) in this way, I can clearly define what the script executed and not executed i.e. everything before the condition has been executed and everything after the line has not. This means when I get an error on line 147, I know immediately what has happened helping me to find a bug faster. Furthermore, if I suddenly realise I need to test $condition2 before $condition1, I can make a change by a simple copy paste.

我看到了很多类似的代码在示例2中,但对我来说,调试起来似乎要复杂得多。这是因为,当嵌套太大时,错误将在底部的某个较远的行处触发,并与大量嵌套代码所导致的情况分开。另外,更改条件序列可能会更加麻烦。

I see a lot of code written like in Example 2 but for me, this seems much more complex to debug. This is because, when the nesting gets too great, an error will get fired off at some distant line at the bottom and be separated from the condition that caused it by a huge chunk of nested code. Additionally, altering the conditional sequence can be a lot messier.

您可以将两种样式混合使用,例如示例3,但这似乎使事情变得过于复杂,因为所有

You could hybrid the two styles, such as in Example 3, but this then seems to overcomplicate matters because all of the 'else's are essentially redundant.

我错过了什么吗?构造条件代码的最佳方法是什么?有没有比这些示例更好的方法? 在某些情况下,一种样式可能优于另一种样式吗?

Am I missing something? What is the best way to structure my conditional code? Is there a better way than these examples? Are there specific situations under which one style may be superior to another?

编辑:示例4看起来很有趣,这不是我考虑过的。您还可以将错误消息作为第二个参数传递。

Example 4 looks quite interesting and is not something I had considered. You could also pass in an error message as a second parameter.

谢谢!

PS。请记住,在检查$ condition1和$ condition2之间,我可能需要执行一些任意步骤,因此任何替代方法都必须适应这一点。否则,会有其他更好的选择,例如if($ condition1 || $ condition2)。

P.S. Please keep in mind that I might need to do some arbitrary steps inbetween checking $condition1 and $condition2 so any alternatives must accommodate that. Otherwise, there are trivially better alternatives such as if($condition1 || $condition2).

推荐答案

我在示例1 营地。根据经验,缩进越少越好。

I am in the Example 1 camp. As a rule of thumb, the less indentation needed, the better.

// Exit early if there are errors.
if ($n < 0) {
    die "bad n: $n";
}

// Handle trivial cases without fuss.
if ($n == 0) {
    return 0;
}

/* Now the meat of the function. */
$widget->frob($n);
foreach ($widget->blaxes as $blax) {
    checkFrobbingStatus($blax);
}
// ...And another 20 lines of code.

当您使用if / else并将成功和错误代码放在并行部分中时,您会看到它好像两个代码块相等。实际上,应该不强调边缘情况和错误条件。通过有意地及早地处理错误,然后 not 将重要的代码放在else子句中,我觉得这样可以使重要代码的位置在视觉上更加清晰。

When you use an if/else and put the success and error code in parallel sections you make it appear as if the two code blocks are equal. In reality, the edge cases and error conditions should be de-emphasized. By intentionally handling errors early and then not putting the "important" code in an else clause I feel like that makes it visually clearer where the important code is.

这里有所有先决条件。现在这是好东西。

"Here are all the preconditions. And now here's the good stuff."

这篇关于构造条件逻辑的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆