开发者

during developement and deployment how assertion statements behave

can any one explain the meanin开发者_Go百科g 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"


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.

boolean debug = false;
assert debug = true;

if (debug) // do something.

OR

assert validate();

// later
private boolean validate() {

     return passed;
}


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".


The assertions in java are enabled by specifying -enableassertions or -ea switch when you run the program. Otherwise, all your assert statements will be ignored. Right here you have a link with an assertions tutorial. Hope to help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜