在开发和部署期间断言语句的行为方式
谁能解释一下“断言让您在开发过程中测试您的假设,但在部署程序时断言代码基本上消失了,没有留下任何开销或调试代码来跟踪和删除”这一说法的含义
can any one explain the meaning of the statement "assertions let you test your assumptions during development but assertion code basically evaporates when the program is deployed leaving behind no overhead or debugging code to track down and remove"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:断言应该用于检测编码错误而不是输入错误。
它们允许您以在打开断言时可以强制执行的方式记录编码中所做的假设。然而,经过良好测试的程序一旦发布就不需要进行这些检查。
因为它们的设计不友好,并且假设修复失败断言的唯一方法是修复代码(通常杀死或禁用进程中的程序)。
它们不适合验证用户输入, >if(!condition)Friendly_user_message() 是一个更好的方法。
断言允许您执行昂贵的测试,您希望能够在生产代码中关闭这些测试。
当您没有打开断言时,JVM 会优化它们。
编辑:如果断言打开,您可能需要执行复杂的检查。有两种方法可以做到这一点。
或者
EDIT: asserts should be used to detect coding errors rather than input errors.
They allow you to document the assumptions made in coding in a way which can be enforced when assertions are turned on. However these checks are ones which a well tested program shouldn't need on once released.
They are not suitable for validating user input as they are not designed to be friendly and assume the only way to fix a failed assertion is to fix the code (often killing or disabling the program in the process)
For validating user input, an
if(!condition) friendly_user_message()
is a better approach.assertions allow you to perform expensive tests you want to be able to turn off for production cocde.
The JVM optimises out the assertions when you don't have them turned on.
EDIT: You may have a complex check you want to perform if asserts are on. Two ways you can do this are.
OR
这通常意味着,由于断言纯粹是一种开发工具,因此您可以在开发时使用 -enableassertions 或 -ea 开关运行虚拟机,这将在运行时执行您的断言,这是“断言让您在开发期间测试您的假设”部分声明。
我假设在您正在阅读的上下文中“部署”(可能是生产?)意味着虚拟机在没有 -enableassertions 或 -ea 的情况下运行,这不会运行您的断言(它只会跳过它们),因此“离开无需开销或调试代码即可追踪和删除”。
It generally means that since assertions are purely a development tool while you develop you can run your VM with the -enableassertions or -ea switches which will execute your asserts during runtime, this is the "assertions let you test your assumptions during development" part of the statement.
I'll assume that in the context of what you're reading "deployment" (production maybe ?) means that the VM is run without -enableassertions or -ea which will not run your assertions (it will just skip them) thus "leaving behind no overhead or debugging code to track down and remove".
java 中的断言是通过在运行程序时指定
-enableassertions
或-ea
开关来启用的。否则,所有assert
语句都将被忽略。在这里你有断言教程的链接。希望能有所帮助。The assertions in java are enabled by specifying
-enableassertions
or-ea
switch when you run the program. Otherwise, all yourassert
statements will be ignored. Right here you have a link with an assertions tutorial. Hope to help.