C#:代码契约与普通参数验证
考虑以下两段代码:
public static Time Parse(string value)
{
string regXExpres =
"^([0-9]|[0-1][0-9]|2[0-3]):([0-9]|[0-5][0-9])$|^24:(0|00)$";
Contract.Requires(value != null);
Contract.Requires(new Regex(regXExpres).IsMatch(value));
string[] tokens = value.Split(':');
int hour = Convert.ToInt32(tokens[0], CultureInfo.InvariantCulture);
int minute = Convert.ToInt32(tokens[1], CultureInfo.InvariantCulture);
return new Time(hour, minute);
}
我
public static Time Parse(string value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
string[] tokens = value.Split(':');
if (tokens.Length != 2)
{
throw new FormatException("value must be h:m");
}
int hour = Convert.ToInt32(tokens[0], CultureInfo.InvariantCulture);
if (!(0 <= hour && hour <= 24))
{
throw new FormatException("hour must be between 0 and 24");
}
int minute = Convert.ToInt32(tokens[1], CultureInfo.InvariantCulture);
if (!(0 <= minute && minute <= 59))
{
throw new FormatException("minute must be between 0 and 59");
}
return new Time(hour, minute);
}
个人更喜欢第一个版本,因为代码更清晰、更小,并且可以轻松关闭合约。但缺点是 Visual Studio 代码分析指责我应该检查参数值是否为 null,并且构造函数的契约没有意识到正则表达式确保分钟和小时在给定的边界内。
因此,我最终收到了很多错误的警告,并且除了 RegEx 验证之外,我看不出有什么方法可以在不抛出 FormatExceptions 的情况下使用合约验证字符串值。
您对如何使用代码合约解决此问题以及类似情况有什么建议吗?
consider the following two pieces of code:
public static Time Parse(string value)
{
string regXExpres =
"^([0-9]|[0-1][0-9]|2[0-3]):([0-9]|[0-5][0-9])$|^24:(0|00)$";
Contract.Requires(value != null);
Contract.Requires(new Regex(regXExpres).IsMatch(value));
string[] tokens = value.Split(':');
int hour = Convert.ToInt32(tokens[0], CultureInfo.InvariantCulture);
int minute = Convert.ToInt32(tokens[1], CultureInfo.InvariantCulture);
return new Time(hour, minute);
}
and
public static Time Parse(string value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
string[] tokens = value.Split(':');
if (tokens.Length != 2)
{
throw new FormatException("value must be h:m");
}
int hour = Convert.ToInt32(tokens[0], CultureInfo.InvariantCulture);
if (!(0 <= hour && hour <= 24))
{
throw new FormatException("hour must be between 0 and 24");
}
int minute = Convert.ToInt32(tokens[1], CultureInfo.InvariantCulture);
if (!(0 <= minute && minute <= 59))
{
throw new FormatException("minute must be between 0 and 59");
}
return new Time(hour, minute);
}
I personally prefer the first version because the code is much clearer and smaller, and the Contracts can be easily turned off. But the disadvantage is that Visual Studio Code Analysis blames that I should check the parameter value for null and the Contracts of the constructor do not realize that the regex ensures that minute and hour are within the given boundarys.
So i end up having a lot of wrong warnings and I see no way to validate string values with contracts without ending up throwing FormatExceptions other than a RegEx validation.
Any suggestions how you would solve this and equivalent situations using Code contracts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了摆脱警告,您可以使用 合同.假设
In order to get rid of warnings you can use Contract.Assume