About Question enthuware.ocpjp.v7.2.1362 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
Kostik
Posts: 2
Joined: Sat Oct 27, 2012 3:35 am
Contact:

About Question enthuware.ocpjp.v7.2.1362 :

Post by Kostik »

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.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1362 :

Post by admin »

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.
That is not correct. The rule is that assertions should not be use to check input parameters of public methods.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

RobynBackhouse
Posts: 23
Joined: Sun Apr 14, 2013 10:37 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1362 :

Post by RobynBackhouse »

In answer 2:

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;       
}
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.

Code: Select all

assert code != 0   //returns true with value which is not zero. e.g. 4
I would have thought that in this case an APPROPRIATE use of an assertion, which is what the question asks, would be

Code: Select all

assert code == 0  //this assertion would fail with any value other than zero
Surely that would be the appropriate use in this case?

renatumb
Posts: 47
Joined: Mon Apr 08, 2013 7:55 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1362 :

Post by renatumb »

I think so, It should be

Code: Select all

assert code == 0

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1362 :

Post by admin »

You are right it should be ==.
It is explained very nicely here: http://docs.oracle.com/javase/7/docs/te ... ssert.html
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;
...
}
This has now been fixed. Thank you for your feedback!
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests