Why statement is not used in deployment

Why is assertion usually not used in deployment? I learned that the statement of arguments of a public method is unacceptable .. but the statement of arguments of a private method is suitable in deployment ..why?

+4
source share
3 answers

Claims are not enabled by default, you need to pass the -ea to the JVM to enable them. Therefore, in many cases, this can be a simple omission to deploy. Other reasons may be productive (I have no evidence that the statements will noticeably slow down) or the correct handling of errors, i.e. It may be considered inappropriate for the production system to choose AssertionError live.

Confirming the arguments of a private method is appropriate because you must have full control over the arguments passed to them. OTOH public methods are called from the outside world, so you can’t control the specific arguments passed, so it’s better to explicitly check the arguments and handle the wrong arguments accordingly (for example, throwing a suitable exception at runtime, such as IllegalArgumentException ), or for null references, let JVM throws a NullPointerException .

+5
source

Claims are disabled by default, since overhead makes them acceptable only to development environments, as they are really useful in finding bugs. In all of your publicly available methods, you should still check the input, not relying on the statements that are allowed (the -e JVM option), so why (or should) be useless.

For your personal methods, on the other hand, you have full control over all the method calls, so you can make sure that you provide the correct arguments, but it's good to say that inside is just to detect any possible errors as early as possible .

+1
source

According to the original book, a pragmatic programmer, statements are disabled by default because compilers are compilers, etc. spread the idea that performance overhead is not valid and only relevant when debugging your code.

In fact, he offered to leave your statements because your testing may not find all the errors, and since a sending chaos monkey can strike at any time. Claims should only be disabled if performance problems occur.

And he also suggested that you should write your own version of assert, which does not necessarily call exit when it fails.

0
source

Source: https://habr.com/ru/post/1341977/


All Articles