断言是邪恶的吗? [英] Is assert evil?

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

问题描述

Go 语言创建者编写:

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.

您对此有何看法?

推荐答案

不,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天全站免登陆