如何避免多重嵌套的IF [英] How to avoid multiple nested IFs
问题描述
我目前正在调整自己的计划,以更加面向对象和更好地实施已知模式等。
I am currently trying to restructure my program to be more OO and to better implement known patterns etc.
我有相当多的嵌套的IF语句,并希望得到摆脱他们。我怎么能去呢?我的第一个方法是把它与例外完成的,所以如
I have quite many nested IF-statements and want to get rid of them. How can I go about this? My first approach was to get it done with exceptions, so e.g.
public static Boolean MyMethod(String param) {
if (param == null)
throw new NullReferenceException("param may not be null");
if (param.Equals("none") || param.Equals("0") || param.Equals("zero"))
throw new ArgumentNullException("param may not be zero");
// Do some stuff with param
// This is not executed if param is null, as the program stops a soon
// as one of the above exceptions is thrown
}
该方法是在主类的应用程序中使用,如:
The method is used in the main class of the application, e.g.
static void Main() {
try {
Boolean test = MyClass.MyMethod(null); // Will throw an exception
} catch (Exception ex) {
MessageBox.Show(ex.Message, "Error");
}
我认为这是相当不错的,因为它可以防止那些语句,几乎所有行动是很好的安排在一个水平的方法。
I think this is quite nice, as it prevents the nested statements and nearly all of the methods actions are nicely arranged on one level.
与IF语句,该方法是这样的。
As with IF-statements, the method would look like this
public Boolean MyMethod(String param) {
if (param != null) {
if (!param.Equals("none") && !param.Equals("0") && !param.Equals("zero")) {
// Do some stuff with param
} else {
MessageBox.Show("param may not be zero", "Error");
} else {
MessageBox.Show("param may not be null", "Error");
}
}
我觉得这非常,非常难看,难以维持
Which I find very, very ugly and hard to maintain.
现在的问题是;这是做法的好的?我知道,这可能是主观的,但你如何克服国际单项体育联合会的嵌套(1级或2级也不差,但之后变得更糟......)
Now, the question is; is this approach good? I know, that might be subjective, but how do you overcome nested IFs (1 or 2 levels are not that bad, but it gets worse after that...)
推荐答案
这真的取决于他们的目的。在第一个样品中,if语句执行服务合同,确保输入的方法满足一定的要求的目的。在这种情况下,我自己的代码往往看起来非常像你的代码。
It really depends on their purpose. In your first sample, the if statements serves the purpose of enforcing a contract, making sure that the input to the method meets certain requirements. In those cases, my own code tend to look pretty much like your code.
在使用if块控制方法的流(而不是强制执行的情况下,合同),它有时会有点困难。有时候我会遇到如下代码(极度简化的)例子:
In the case of using the if blocks to control the flow of a method (rather than enforcing a contract), it can sometimes be a bit harder. Sometimes I come across code like the following (extremely simplified) example:
private void SomeMethod()
{
if (someCondition == true)
{
DoSomething();
if (somethingElse == true)
{
DoSomethingMore();
}
}
else
{
DoSomethingElse();
}
}
在这种情况下,它好像该方法具有身兼数职,所以在这种情况下,我可能会选择将其分割成几个方法:
In this case, it seems as if the method has several responsibilities, so in this case I would probably choose to split it into several methods:
private void SomeMethod()
{
if (someCondition == true)
{
DoItThisWay();
}
else
{
DoSomethingElse();
}
}
private void DoItThisWay()
{
DoSomething();
if (somethingElse == true)
{
DoSomethingMore();
}
}
这使得每个方法多用更少的嵌套代码更简单,并且还可以提高可读性,如果方法是给定好名称
This makes each method much simpler with less nested code, and may also increase the readability, if the methods are given good names.
这篇关于如何避免多重嵌套的IF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!