寻找类不变量的启发式方法

发布于 2024-09-10 14:42:29 字数 1539 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不交电费瞎发啥光 2024-09-17 14:42:29

类之间差异很大,启发式方法很难获得。不过,对于特定风格的课程,会有很多启发式的方法。例如,每个设计模式实现都有自己的一组不变量。

获得不变量的最好、最简单的方法是从类应该做什么的规范开始[或者如果进行 TDD,则进行测试测试],并找出要正确实现这些规范必须满足哪些条件。换句话说,您“通过代码推动您的规范”。当您这样做时,不变量将“出现”为您遇到的绊脚石。

请注意,有时“推动”意味着前置条件的前向传播,有时意味着后置条件的向后传播。大多数情况下,两者都是。

classes vary so much from each other, heuristics will be hard to come by. There will be lots of heuristics for particular styles of classes though. For example, each design pattern implementation has its own set of invariants.

The best and simplest way to get your invariants is to start from a specification of what your class is supposed to do [or a test of tests if you do TDD], and figure out what conditions must be true for those specifications to be properly implemented. In other words, you "push your specifications through your code". As you do this, invariants will 'appear' as the stumbling blocks you encounter.

Note that sometimes "pushing through" means forward propagation of pre-conditions, sometimes it means backwards propagation of post-conditions. Most often, both.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文