断言邪恶?
Go
语言创作者写:
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.
- 断言:程序逻辑本身失败。
- 错误处理:错误的输入或系统状态不是由于程序中的错误。