断言邪恶? [英] Is assert evil?

查看:134
本文介绍了断言邪恶?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Go 语言创作者写< a>:

The Go language creators write:


Go不提供断言。他们无疑是方便的,但我们的经验是程序员将它们用作拐杖,以避免考虑正确的错误处理和报告。正确的错误处理意味着服务器在非致命错误而不是崩溃后继续运行。正确的错误报告意味着错误是直接的,至此,节省程序员解释大的崩溃痕迹。程序员看到错误不熟悉代码时,精确的错误尤其重要。

Go doesn't provide assertions. They are undeniably convenient, but our experience has been that programmers use them as a crutch to avoid thinking about proper error handling and reporting. Proper error handling means that servers continue operation after non-fatal errors instead of crashing. Proper error reporting means that errors are direct and to the point, saving the programmer from interpreting a large crash trace. Precise errors are particularly important when the programmer seeing the errors is not familiar with the code.

你对此有何看法? p>

What is your opinion about this?

推荐答案

不,只要你使用它,只要 assert

No, there's nothing wrong with assert as long as you use it as intended.

也就是说,它应该是在调试期间捕获不能发生的情况,而不是正常的错误处理。

That is, it's supposed to be for catching cases that "can't happen", during debugging, as opposed to normal error handling.


  • 断言:程序逻辑本身失败。

  • 错误处理:错误的输入或系统状态不是由于程序中的错误。

这篇关于断言邪恶?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆