Hi all!
Here us a question which asks about appropriate usage of assertions.
There are 5 answers, three of them shows how assertions are used (source code provided, assertions are used in public methods) and 4th and 5th:
4) none of the above is appropriate.
5) all of the above are appropriate.
Software says, that correct answer is 5, but I would suppose that 4, because sources provided public methods and it is not good practice to use assertions in public methods.
About Question enthuware.ocpjp.v7.2.1362 :
Moderators: Site Manager, fjwalraven
-
- Posts: 2
- Joined: Sat Oct 27, 2012 3:35 am
- Contact:
-
- Site Admin
- Posts: 10057
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1362 :
That is not correct. The rule is that assertions should not be use to check input parameters of public methods.Kostik wrote: Software says, that correct answer is 5, but I would suppose that 4, because sources provided public methods and it is not good practice to use assertions in public methods.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 23
- Joined: Sun Apr 14, 2013 10:37 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1362 :
In answer 2:
The if checks that the value of code is equal to zero, and processes it if it is, and the else - asserts true if the value is not zero.. So the assertion only fails if the value actually IS zero, but we have already processed that value as being the correct one we want.
If the value of code is for example 4, then the assertion returns true, so does not throw the assertionError.
I would have thought that in this case an APPROPRIATE use of an assertion, which is what the question asks, would be
Surely that would be the appropriate use in this case?
Code: Select all
int code = getCode(); // always returns 0
if (code == 0) {
// deal with 0 ...
} else {
assert code != 0 : "Can't process because code is out of range -"+code;
}
If the value of code is for example 4, then the assertion returns true, so does not throw the assertionError.
Code: Select all
assert code != 0 //returns true with value which is not zero. e.g. 4
Code: Select all
assert code == 0 //this assertion would fail with any value other than zero
-
- Posts: 47
- Joined: Mon Apr 08, 2013 7:55 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1362 :
I think so, It should be
Code: Select all
assert code == 0
-
- Site Admin
- Posts: 10057
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1362 :
You are right it should be ==.
It is explained very nicely here: http://docs.oracle.com/javase/7/docs/te ... ssert.html
Paul.
It is explained very nicely here: http://docs.oracle.com/javase/7/docs/te ... ssert.html
This has now been fixed. Thank you for your feedback!Internal Invariants
Before assertions were available, many programmers used comments to indicate their assumptions concerning a program's behavior. For example, you might have written something like this to explain your assumption about an else clause in a multiway if-statement:
if (i % 3 == 0) {
...
} else if (i % 3 == 1) {
...
} else { // We know (i % 3 == 2)
...
}
You should now use an assertion whenever you would have written a comment that asserts an invariant. For example, you should rewrite the previous if-statement like this:
if (i % 3 == 0) {
...
} else if (i % 3 == 1) {
...
} else {
assert i % 3 == 2 : i;
...
}
Paul.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 14 guests