基本防御性编程

发布于 2024-08-16 02:24:08 字数 329 浏览 6 评论 0原文

可能的重复:
最喜欢的(聪明的)防御性编程最佳实践

一些程序员总是建议我专注于轻松调试。什么是防御性编程以及练习时应在多大程度上考虑它?

还有一个更重要的问题:编码时是否需要考虑任何关键事项?它们是什么?

Possible Duplicate:
Favorite (Clever) Defensive Programming Best Practices

I am always advised by some programmers to pay concentration to easy debugging. What is defensive programming and to which extend should it be considered while practicing?

And one more important question: is there any key things to consider while coding and what are they?

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

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

发布评论

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

评论(4

胡渣熟男 2024-08-23 02:24:08

看看

防御性编程的思想是
开发人员做出尽可能少的假设
绝对有必要。此外,
开发者抢先创建
代码不仅预测
还存在潜在的问题
规格变更。

Have a look at

Defensive programming is the idea that
the developer makes as few assumptions
as absolutely necessary. In addition,
the developer preemptively creates
code that anticipates not only
potential problems but also
specification changes.

梦巷 2024-08-23 02:24:08

根据经验,如果您发现自己在想“这将永远是正确的”,请在该位置写入 ASSERT(condition)。这可能是防御性编程的核心;)。

As a rule of thumb -- if you catch yourself thinking "this will always be true", write ASSERT( condition) in that place. That is probably the core of what defensive programming should be ;).

情痴 2024-08-23 02:24:08

如果防御性编程只意味着一件事,那就应该广泛使用断言

这里是一篇关于何时和在哪里使用断言

有很多情况
使用断言是很好的。这
部分涵盖其中一些:

* 内部不变量
* 控制流不变量
* 前置条件、后置条件和类不变量

If defensive programming meant only one thing , that should be use assert extensively.

Here is a good article about when and where to use assert.

There are many situations where it
is good to use assertions. This
section covers some of them:

* Internal Invariants
* Control-Flow Invariants
* Preconditions, Postconditions, and Class Invariants
策马西风 2024-08-23 02:24:08

http://en.wikipedia.org/wiki/Defective_programming

防御性编程意味着您检查文件是否存在并且您是否有权打开它,而不是仅仅尝试打开它并捕获任何最终的异常。
(仅举个例子)

http://en.wikipedia.org/wiki/Defensive_programming

Defensive programming means, that you check if a file exists and if you have the permissions to open it instead of just trying to open it and catching any eventual exceptions.
(Just an example)

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