2

I stumbled upon an article named Programming With Assertions.

And beside the mechanism of turning on and off assertions after compile time, I don't get it. Why were assertions introduced on language level, which is quite a big deal, isn't it?

I have never used assertions in production code, was never taught to use them in production code, and never saw assertions in production code. Beyond that, I would always prefer a more specific Exception over an AssertionError.

So here are my questions. Does anyone use assertions in production code, and what are the advantages of that usage?

4
  • 1
    What do you mean by "productive code"? Do you mean "production code"? Commented Mar 6, 2013 at 9:46
  • I guess I meant production code, contrary to code that is not run by Users like unit tests. Commented Mar 6, 2013 at 10:02
  • @gnat Thanks for providing that Q&A. Didn't found that. But I think the question is more focused about how it was intended to being used, as I am interested in advantages of one over the other. Commented Mar 6, 2013 at 10:49
  • 1
    also related programmers.stackexchange.com/questions/138951/…
    – jk.
    Commented Mar 6, 2013 at 12:11

2 Answers 2

1

Assertions are used to assert that a condition is always true. They're used in code to ensure that an assumption is correct.

Generally if you see an assertion error something bad has gone wrong.

You can read all about them at Wikipedia.

When it comes to using your own assertions, don't use them in place of exceptions. Instead, use them wherever you feel the need to assert that a condition always holds true, for example a value you may rely on in an internal data structure.

3
  • As to your last paragraph, wouldn't an IllegalStateException indicate such an example? Commented Mar 6, 2013 at 10:08
  • No - from the JDK documentation - "Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an appropriate state for the requested operation."
    – Sam
    Commented Mar 6, 2013 at 10:27
  • An Assert is used for data structures, IllegalStateException is used to signal a method.
    – Sam
    Commented Mar 6, 2013 at 10:28
0

Well technically speaking an assert normally gets compiled out of release builds so you wouldn't see them operate in code running in production. That said, you may still put them in to your application code as a kind of inline unit test so that they still do operate whilst your code is running in development/testing.

Normally unit tests run bits of application code and then check that certain things are true using asserts. That said there is still sometimes a reason to use an assert within the application code because, most importantly, a unit test can fail from an assert in the unit test code or an assert in the application code being tested.

Consider that you write a unit test that runs a contrived sequence of operations on your application that you suspect could theoretically cause bad things to happen. You might not have an easy way, from the scope unit test, to probe into your application classes to check if something is going wrong and there might be an easier alternative in adding the assert into the application code directly. Sometimes I write unit tests without an assert in the unit test itself, simply to attempt (and hopefully fail) to invoke one that I put in the application.

And a final note - asserts are not the same as exceptions. Exceptions are used for still expected (rare) behaviour and this can still be tested for as unit tests can be written to expect exceptions. Assertions are used for impossible unexpected behaviour and hence associated with the core concept of unit testing.

2
  • in Java, saying "assert normally gets compiled out of release builds" makes no sense. The very tutorial article mentioned in the question (did you read it?) states: "command-line switches allow you to selectively enable or disable assertions"
    – gnat
    Commented Mar 6, 2013 at 12:36
  • Admittedly, whilst reading programmers.se I kind of put my language-agnostic hat on and tend to ignore the language tags so please take that statement for its general meaning.
    – Benedict
    Commented Mar 6, 2013 at 13:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.