在方法开始和结束时进行错误处理的优缺点是什么
根据我的程序员的经验,我混合了所有可能的错误处理方式......我创建了我的个人风格。
不过,我想听听您认为在方法开始和结束时进行错误处理的优缺点。
开始时处理:
public String GenerateSomeStringData(String data, int value)
{
if (data == null)
throw new ArgumentNullException("data");
if (value <= 0)
throw new ArgumentException("value must be greater than zero");
int dataValue;
if (!int.TryParse(data, out dataValue))
throw new InvalidOperationException("data does not contain an integer");
return dataValue * 4 + value / 12;
}
最后处理:(同一示例)
public String GenerateSomeStringData(String data, int value)
{
if (data != null)
{
if (value > 0)
{
int dataValue;
if (int.TryParse(data, out dataValue))
{
return dataValue * 4 + value / 12;
}
else
throw new InvalidOperationException("data does not contain an integer");
}
else
throw new ArgumentException("value must be greater than zero");
}
else
throw new ArgumentNullException("data");
}
在决定如何处理此问题时,您使用什么标准?可读性、可维护性、简洁性?
In my programmer's experience, I have mixed error handling all the ways possible... I have created my personal style.
However, I'd like to hear what you consider to be the pro and cons of error handling at the beginning vs at the end of the method.
Handling at the beginning:
public String GenerateSomeStringData(String data, int value)
{
if (data == null)
throw new ArgumentNullException("data");
if (value <= 0)
throw new ArgumentException("value must be greater than zero");
int dataValue;
if (!int.TryParse(data, out dataValue))
throw new InvalidOperationException("data does not contain an integer");
return dataValue * 4 + value / 12;
}
Handling at the end: (the same example)
public String GenerateSomeStringData(String data, int value)
{
if (data != null)
{
if (value > 0)
{
int dataValue;
if (int.TryParse(data, out dataValue))
{
return dataValue * 4 + value / 12;
}
else
throw new InvalidOperationException("data does not contain an integer");
}
else
throw new ArgumentException("value must be greater than zero");
}
else
throw new ArgumentNullException("data");
}
What criteria do you use when deciding how to approach this? Readability, maintainability, brevity?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
输入的有效性应该是执行该方法的先决条件 - 因此我总是(并且这样做) 首先进行错误处理。
这具有以下优点:
对于人类来说更容易解析:
首先验证前提条件,然后
执行逻辑(通常
导致某些后置条件)
之间的关注点清晰分离
错误处理和执行逻辑
在你的方法中:验证逻辑没有“散布”在执行逻辑中
正如评论中提到的,你必须区分违反前提条件并触发错误条件(例如抛出异常)的无效输入)和构成边缘条件的有效输入(即需要一些特殊逻辑来处理)。后一种情况我将在断言前提条件后、在方法的执行逻辑开始时单独处理。
Validity of input should be a precondition for execution of the method - as such I would (and do) always do the error handling first.
This has the following advantages:
It is easier to parse for a human:
validating preconditions first, then
execution logic (which usually
results in some post condition)
Clear separation of concerns between
error handling and execution logic
within your method: validation logic is not "sprinkled in" within the execution logic
As mentioned in the comments you have to differentiate between invalid input that violates a precondition and triggers an error condition (such as throwing an exception) and valid input that constitutes an edge condition (i.e. requiring some special logic to handle). The later case I would handle separately after asserting the preconditions, at the beginning of the execution logic of your method.
正如其他人提到的,我会在执行该方法的任何核心逻辑之前检查输入有效性。这不仅具有逻辑意义,而且还减少了代码的缩进级别,并保证您没有任何 if {} 语句太长,以至于在一个屏幕上看不到 else
As others have mentioned, I would check for input validity before executing any core logic of the method. Not only does this make logical sense but it also reduces the levels of indentation of your code and guarantees you don't have any if {} statements that are so long you can't see the else on one screen
第一种方法要好得多。
它可以帮助您将所有验证保存在一处。您甚至可以编写一个通用函数来处理此类验证。
绝对更具可读性。您一开始就完成了所有验证,因此您可以继续执行实际逻辑。
如果您的代码跨度有点大,则很难找到 if 循环的右括号。
The first method is much better.
It helps you keep all your validations in one place. You could even write a generic function to handle this type of validations.
Definitely more readability. You are done with all your validations in the beginning so you could go about your actual logic.
If your code spans a little bit more, it will be tough to track down the closing parentheses of the if loops.
我的意见是:首先进行错误条件检查,以便其他人清楚地定义什么对你的方法是可以的,什么是不行的。
My opinion is: do error condition check first for clear definition for someone else about what is OK for your method and what is NOT ok.