Thursday, 8 August 2013

Confusion - Assertion Appropriate Usage

Confusion - Assertion Appropriate Usage

I am taking this code snippet from K&B practice exams.
public class Later {
public static void main(String[] args) {
boolean earlyExit = new Later().test1(args);
if (earlyExit) assert false; // LINE 5
new Later().test2(args);
}
boolean test1(String[] a) {
if (a.length == 0) return false;
return true;
}
private void test2(String[] a) {
if (a.length == 2) assert false; // LINE 13
}
}
The answer in K&B states that, LINE-5 AND LINE-13 are in-appropriate use
of assertions. I would like to know why. Here is my observation after
reading topic of assertion from K&B.
1.I do understand, LINE 5 is in-appropriate because it is using assertion
to validate command line arguments. Assertions should never be used to
validate command line arguments.
2.In answer, it also states that, LINE 13 is also in-appropriate use of
assertions. Good practice in assertions states that, you can use
assertions to validate arguments to your private methods. So my question
is why LINE 13 is in-appropriate use of assertions.
Any insights over this will be helpful.

No comments:

Post a Comment