8

I wonder if a lot of people program in Java with assertions. I think this can be very useful on large projects without enough written contracts or outdated contracts. Particularly when you use webservices, components, etc.

But I have never seen any project using assertions (except in JUnit/testing tests...).

I've noticed that the thrown class is an Error and not an Exception. Why do they choose an error? Can it be because an exception could be unexpectedly caught and not logged/rethrown?

If you develop an application with components, I wonder where you put the assertions:

  • On the component side, just before returning the data through the public API?
  • On the component client side? And if the API is called everywhere you set up a facade pattern that will call the assertion mechanism? (Then I guess you put your assertions and facade on some external project and your client projects will depend on this assertion project?)

I understand how to use assertions, and when use them, but just wonder if some people have recommendations based on a real experience of assertions.

4 Answers 4

5

By the way, do you refer to assert in Java?

I personally find assertions especially useful for invariants. Take into account that assertion checking is turned off by default in Java. You have to add the -ea flag to enable assertion checking.

In other words, you can test your application in a kind of debug mode, where the program will halt once an assertion is broken. On the other hand, the release application will have its assertion turned off and will not incur time penalty for assertion checking. They will be ignored.

In Java, assertions are far less powerful than exceptions and have totally different meanings. Exceptions are there when something unexpected happens and you have to handle it. Assertions are about the correctness of your code. They are here to confirm that what 'should be' is indeed the case.

My rough policy, especially when working with many developers:

  • public methods: always check arguments and throw IllegalArgumentException when something is wrong
  • private methods: use assertions to check arguments against null pointers and so on
  • complex methods: intermediate assertions to ensure that the intermediate results satisfy requested properties

...but actually, I use them sparsingly. Just where it's critical or error-prone places.

Sign up to request clarification or add additional context in comments.

2 Comments

have you ever turned assertions in productions?
I tend to disable them in production and use them solely for testing/debugging...
4

About the minor usage of asserts, I think that was a bad decision to make assertions disabled by default.

About extending Error I suppose it extends Error because Errors are exceptions that are not expected to be caught. And that way, when in your code you have catch(Exception), the assertion isn't cached.

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

Comments

1

In my opinion, errors in Java should be treated as an Exception. Therefore I would enable assertions in development and in private methods to check that my code is running fine and don't pass invalid values to private methods.

Since those checks should be made in public methods, I wouldn't not check again in private methods.

In order to disable assertions:

-da flag in compiler

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

6 Comments

Why disabling it in production? I would keep one instance with assertions enabled because somestimes we use components (from us or third party) that could have different configurations in production and then contracts could be broken only in production env...
Assertions verify contracts and there can be contracts in public methods so... ? Please don't tell me what you'll do without explaining why ;) Can't we consider a broken contract could be a different problem that a programming problem? An app could continue running with a broken contract and disabled assertions, while once you've set an exception to verify a contract, you just have to make another delivery... And this contract can be broken at any moment (ex you call an url providing data through httpclient, but the url you call have been updated and some data is missing...)
And what's the point of doing assertions only in private methods? Then i'll put all my code in private methods with assertions and make public methods that just call the private methods and it would be ok for you?
Well, if there is anything i think it can fail at runtime I would check it and then manage the error as an exception or just log it. When I say private methods I'm thinking in a piece of code which I control and feed with parameters. If those parameters comes from an external system it had passed through a public method. Since those parameters might have invalid values I would check them there, and don't check again in the private method. Sorry for my poor english, and once again, it is just my opinion.
for sure you always check params with exceptions... it's not the point of assertions, but anyway i don't see anything wrong in checking invariant in public methods. It's not because a method is public that you can't assert anything else than the methods parameters.
|
-1

Assertions should not be used outside tests because they can be turned off in a production environment, which may cause serious problems due to the lack of proper checking.

However, I've seen statement that it is allowed to use them to check parameters in private methods. That's because you assume that data which managed to reach to your private method is correct and if it isn't application may fail hard.

1 Comment

Actually my point has never been to replace exceptions by assertions. Turning assertions off would not change the behaviour of my app...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.