Java - statement programming

I wonder how many people program in Java with statements. I think this can be very useful on large projects without sufficient written contracts or outdated contracts. Partly when using web services, components ...

But I have never seen a single project using assertions (except for junit / testng tests ...).

I noticed that an abandoned class is a mistake, not an exception. Can someone tell me why they choose the mistake? Could this be due to the fact that the exception could be caught unexpectedly, and not the logger / reorganized?

If you are creating an application with components, I wonder where you put the statements: - On the component side, just before the data is returned via the public api? - On the client component side? And if api is called everywhere, are you customizing the facade template that will trigger the approval mechanism? (Then, I suppose, you put your statements and facade on some external project, and your client projects will depend on this approval project?)

I understand how to use statements and use them, but it's just interesting if some people have recommendations based on the actual experience of the statement.

thanks

+7
source share
4 answers

Btw, are you referring to assert in java?

I personally find statements especially useful for invariants. Note that claims validation is disabled by default in java. You must add the -ea flag to enable claims validation. In other words, you can test your application in some kind of debugging mode when the program is stopped as soon as the statement is broken. On the other hand, the release application will disable its approval and will not incur a time penalty for checking claims; they will be ignored.

In java, statements are much less efficient than exceptions, and have completely different meanings. There are exceptions when something unexpected happens and you have to deal with it. Statements are related to the correctness of your code. They are here to confirm that the β€œmust be” is true.

My rough policy, especially when working with many developers:

  • public methods: always check the arguments and throw an IllegalArgumentException when something is wrong.
  • private methods: use statements to test arguments against null pointers, etc.
  • sophisticated methods: intermediate statements to ensure that intermediate results satisfy the requested properties

... but actually I use them sparsely. Exactly where it is critical or error prone places.

+4
source

About the minor use of statements I think it was a bad decision to disable the default statements.

About the Error Extension Assume that it extends Error because errors are exceptions that are not expected. And so, when you have a catch (exception) in your code, the statement is not cached.

And about use, the best place is in prerequisites, postconditions or in the middle of the code in any invariant that you want to check.

+3
source

In my opinion, errors in Java should be excluded as an exception. Therefore, I would include statements in development and in private methods to verify that my code is working fine and not to pass invalid values ​​to private methods.

Since these checks must be performed in public methods, I would not check private methods again.

To disable claims:

-da flag in compiler

In my opinion, in public methods you should check and manage the exception or write them yourself.

0
source

Claims should not be used outside the tests because they can be disabled in the production environment, which can cause a serious problem due to the lack of proper validation.

However, I saw an expression saying that it is allowed to use them to check parameters in private methods, because you assume that the data that could be found in your private method is correct, and if the isnt application may not work much.

-one
source

All Articles