2

Possible Duplicate:
What does assert do?

What is "assert"? What for is "assert" key-word used for? When and where can it be useful?

This is an example of method from red-black tree implementation:

public Node<K,V> grandparent() {
    assert parent != null; // Not the root node
    assert parent.parent != null; // Not child of root
    return parent.parent;
}

i don't know what for "assert" is used in this code. Can't we type this code in some other way, with - for example - "if's" instead?

Community
  • 1
  • 1
Ariel Grabijas
  • 1,472
  • 5
  • 25
  • 45

5 Answers5

4

Assertions are used to test assumptions about running code. They differ from if checks in a number of ways:

  • Assertions are disabled at runtime, by default
  • Assertions shouldn't be used for argument checking, or anything else that is required for normal operation of your program.
  • Assertions often test assumptions about state you might have previously seen in a comment. For example, you might use an assertion to verify that you're not violating a loop invariant or that a method's preconditions have been satisfied.

if statements are used for control flow, argument checking, etc. Assertions are used to reason about the correctness of internal methods, etc, during development and debugging.

In practice, these two lines:

assert parent != null; // Not the root node
assert parent.parent != null;

...enforce that neither parent or parent.parent is null when this method is called. If either of those assertions fail, the program will throw an AssertionError.

Wayne
  • 59,728
  • 15
  • 131
  • 126
  • 1
    Also note that it's pretty likely that the author of that snippet *mis*used `assert`. As you say, assertions are *not* for argument checking. –  Dec 10 '11 at 22:55
  • So the method above is example of wrong assertion application. – Ariel Grabijas Dec 10 '11 at 23:11
  • In fact, this use of `assert` is not "wrong," but it may not be quite as useful as it might be. If the parent `parent` is `null`, it will throw a NullPointerException in the return statement when it is dereferenced. Therefore, the first assertion serves more as documentation than a practical means by which a `null` value can be detected during development and testing. The second `assert` statement will "fail early," which can be useful rather than waiting for a later failure which may need more work to trace back. However, the `assert` statements are simply documentation for production code. – Ned Mar 11 '14 at 20:45
3

assert parent != null will throw an AssertionError if the condition (parent != null) is false. This can be used for detecting bugs in your application.

You could do the same with an if, but asserts can be enabled or disabled at runtime with the -ea JVM parameter. When they're disabled they have 0 impact on performance, while ifs are always executed.

Of course, that does mean you shouldn't use them for critical checks, such as those that check for exceptional user input or IO errors. Most of the time you'll want to use an if anyway, unless they have a significant performance impact.

Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301
2

From the Java Language Specification, section 14.10:

An assertion is a statement containing a boolean expression. An assertion is either enabled or disabled. If the assertion is enabled, evaluation of the assertion causes evaluation of the boolean expression and an error is reported if the expression evaluates to false. If the assertion is disabled, evaluation of the assertion has no effect whatsoever.

Follow the link for more details.

Personally I usually prefer checks which are always enabled - I dislike the behaviour of code changing (in a dangerous way, i.e. allowing execution to continue in odd situations) in production. It's a bit like deciding to wear a seat-belt when driving slowly, but taking it off for a race...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • About the last paragraph: While I agree that you can get some really hard to find bugs there (the classic `assert mutatingFunction()`), I like being able to put some really expensive tests into assertions (i.e. a function that checks integrity of some large data structure) which I really couldn't do in production code. On the other hand another problem I see with assertions is that they lead to programmers not worrying about error handling in situations where they clearly should. – Voo Dec 10 '11 at 23:02
1

Assert is a debugging tool: it tests for a condition and throws an AssertionError if the condition does not hold. You can use it to check whether preconditions, invariants and postconditions of your algorithm hold. Don't use it for input validation; it's meant to catch programmer errors.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
0

Asserts in this case are used to ensure preconditions are true. For details, see design by contract

GETah
  • 20,922
  • 7
  • 61
  • 103