正确使用Java“assert”关键字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18907487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Correct use Java "assert" keyword
提问by Christopher Francisco
I have never understood what is assert
used for, even though I have read plenty examples, they don't really let me know what or why should I use it for.
我从来不明白是assert
用来做什么的,尽管我已经阅读了很多例子,但它们并没有真正让我知道我应该用它做什么或为什么要使用它。
So Instead of asking an example, I'm gonna provide one and let me know if this is the proper usage of assert
.
所以我不会问一个例子,而是提供一个例子,让我知道这是否是assert
.
// The idea is that the `mode` variable should be 0 or 1, and no other number.
switch(mode) {
case 0:
// do stuff
break;
case 1:
// do other stuff
break;
default:
// assert code?
}
If this is correct, please let me know how to use it in this case. If this is not how it is supposed to use, please provide an example.
如果这是正确的,请告诉我在这种情况下如何使用它。如果这不是它应该使用的方式,请提供一个示例。
采纳答案by Makoto
Not in this case.
在这种情况下不是。
If you're asserting a value, you're making a statement that, before some critical evaluation is done using this value, that it is what you assert it to be. You can assert that the value isn't null, or that it's less than 2, or something beforeyou reach your critical code block.
如果您要断言一个值,那么您就是在声明,在使用此值完成某些关键评估之前,它就是您断言它的样子。在到达关键代码块之前,您可以断言该值不为空,或者它小于 2,或者其他东西。
assert (mode >= 0 && mode < 2); // Ensures that `mode` is between 0 and 1.
// Switch statement to follow
I would not encourage the use of that here. Your code would not read well, and unless you enable assertions with the -ea
flag, your assertion would not work.
我不鼓励在这里使用它。您的代码不会很好读,除非您启用带有-ea
标志的断言,否则您的断言将无法工作。
Instead, what you can do is throw an exception of some kind - if it's not 0 or 1, then the mode
is an illegal value which cannot be processed, leading to exceptional/undefined behavior. Throw an exception of some kind.
相反,您可以做的是抛出某种异常 - 如果它不是 0 或 1,那么它mode
是一个无法处理的非法值,从而导致异常/未定义的行为。抛出某种异常。
switch(mode) {
case 0:
// do stuff
break;
case 1:
// do other stuff
break;
default:
throw new IllegalArgumentException("Mode is illegal");
}
回答by Rami
Assertions are basically used to check something that should never happen. Some assertions use cases from http://www.javapractices.com/topic/TopicAction.do?Id=102
断言基本上用于检查不应该发生的事情。一些断言用例来自http://www.javapractices.com/topic/TopicAction.do?Id=102
pre-conditions (in private methods only) - the requirements which a method requires its caller to fulfill. post-conditions - verify the promises made by a method to its caller class invariants - validate object state unreachable-at-runtime code - parts of your program which you expect to be unreachable, but which cannot be verified as such at compile-time (often else clauses and default cases in switch statements)
先决条件(仅在私有方法中) - 方法要求其调用者满足的要求。后置条件 - 验证方法对其调用者类不变量所做的承诺 - 验证对象状态在运行时无法访问代码 - 您希望无法访问但无法在编译时验证的程序部分(通常在 switch 语句中使用 else 子句和默认情况)
So the usage of assertion in your code is not correct
所以在你的代码中使用断言是不正确的
回答by Terry Li
assert object != null;
object.doSomething();
assert
is used to verify the correctness of some precondition, invariant, or postcondition. In the example, we want to make sure object
is not null
when some method is called on it.
assert
用于验证某些前置条件、不变量或后置条件的正确性。在示例中,我们要确保object
不是null
在调用某个方法时。
One thing to remember is that assert
should never be executed in production code. We only make use of it when testing. There is a Java option to turn it on or off.
要记住的一件事是assert
永远不应该在生产代码中执行。我们只在测试时使用它。有一个 Java 选项可以打开或关闭它。
As for your specific example, you could use:
至于你的具体例子,你可以使用:
assert mode == 0;
assert mode == 1;
at the very beginning of the switch block to make sure only 0
and 1
are passed in.
在 switch 块的最开始确保 only0
和1
被传入。
P.S. The discussion on when to use assertion vs exceptionmight help your understanding. The idea is that
PS 关于何时使用断言与异常的讨论可能有助于您的理解。这个想法是
Exceptions address the robustness of your application while assertions address the correctness of your application.
异常解决了应用程序的健壮性,而断言解决了应用程序的正确性。