使用 BDD 时遵循开放/封闭原则有什么好处吗?

发布于 2024-10-14 13:41:30 字数 108 浏览 7 评论 0原文

开放/封闭原则似乎是为了防止对象或方法中的回归。鉴于您的代码已被测试覆盖,因为您正在练习 BDD,这似乎是一个多余的要求。此外,它似乎通过要求 API 级别而不是语言级别的可扩展性而引入了额外的复杂性。

The open/closed principle seems to be about preventing regressions in an object or method. Given that your code is covered by tests because you're practicing BDD this seems a redundant requirement. In addition it seems to introduce additional complexity by requiring extensibility at the API level rather than the language level.

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

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

发布评论

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

评论(1

你不是我要的菜∠ 2024-10-21 13:41:30

绝对有好处。事实上,这两个原则(BDD 和开放/封闭)是为了不同的目的而设计的。 BDD 旨在引导开发过程,这就是它的好处(缩短时间、制作更高质量的代码等)。开放/封闭旨在在开发过程中完成,但有助于维护。

BDD 的好处很容易掌握。初始开发时间较短意味着整个项目的成本较低,对吗?错误的。根据60/60 规则,60% 的成本项目的成本来自维护(其中 60% 的成本来自部署后的需求变更)。因此,虽然在最初的开发阶段节省资金是有利的,但在维护期间可以节省更多的资金。

这就是开放/封闭本金将得到回报的地方。通过遵循该原则,您将为自己节省大量维护时间(因为您不需要因为更改方法的功能而跟踪损坏的单元测试)。

开放/封闭原则并不是为了防止回归,而是为了防止 API 发生变化,而这种变化几乎是不可能跟上的。如果您注意到的话,好的 API 永远不会改变。它们可能会被延长。某些部件可能会被弃用。但您永远不会看到 setFoo(string bar) 更改为 setFoo(int bar)。这就是 Open/Closed 所要防止的......

Absolutely there are benefits. In fact, the two principals (BDD and Open/Closed) are designed for different purposes. BDD is designed to lead the development process, and that's where its benefits are felt (shortening timelines, making higher quality code, etc). Open/Closed is designed to be done in the development process, but help in maintenance.

The benefits of BDD are easy to grasp. Shorter timelines for initial development mean less cost for the project as a whole, right? Wrong. Based upon The 60/60 Rule, 60% of the cost of the project is from maintaining it (and 60% of that cost is from requirements changes post-deployment). So while it's beneficial to save money during the initial development phase, the bigger savings is to be had during maintenance.

And that's where the Open/Closed principal will pay off. By following that principal, you will save yourself a lot of time in maintenance (since you don't need to track down broken Unit Tests because you change the functionality of a method).

And the Open/Closed principal isn't about preventing regressions so much as it is preventing a changing API which is almost impossible to keep up with. If you notice, good APIs never change. They may be extended. Parts may be deprecated. But you never see setFoo(string bar) change to setFoo(int bar). That's what Open/Closed is there to prevent...

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