Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

参数约束一例 #18

Open
hax opened this issue Mar 23, 2015 · 0 comments
Open

参数约束一例 #18

hax opened this issue Mar 23, 2015 · 0 comments
Labels

Comments

@hax
Copy link
Owner

hax commented Mar 23, 2015

http://www.zhihu.com/question/28940236/answer/42664451

DateTime.Today().Add(new TimeSpan(365,0,0,0))……写代码的哥们写成new DateTime(today.Year+1,today.Month,today.Day)。
经过各种测试运行一直都没问题。直到2012年2月29日,ArgumentOutOfRangeException。

看了这个贴除了会心一笑,又陷入思考。

我一下想到了JavaScript中的new Date的API,面临相同的代码,会容错,自动进位到3月1日。这样一种API设计是否好呢?

看上去这样的设计能避免出现该答案中服务挂了若干小时的问题。但是仔细想来,进位到3月1日从业务逻辑上讲未必符合,只是此case中时间的精确性并不重要。然而如果是金额非常巨大的金融系统,在闰年自动进位的一天或许就成了一个潜藏的漏洞。由此考虑,容错这件事情其实未必是好事。

下面有评论说此问题是测试不到位,测试应该注意边界条件。我觉得有一点道理,但从工程角度来说,如何让测试能避免这种遗漏?

这就想到之前看到的一些自动产生测试数据的方案。顺便提及,这也需要类型系统支持,只有知道传入参数是日期类型(甚至更强的范围约束),才能自动产生对应的测试数据。

BTW,这个例子又让我隐隐想起checked exception和unchecked exception的争议。在这个例子里也许编译器强制要求检查是否捕捉了可能的exception是更好的选择?

@hax hax added the 笔记 label Mar 23, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant