• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Asserting arguments of public method

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain why it is not appropiate to assert arguments of public method as precondition? Why it is okay for say private method?
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the assertions were not enabled the checks that they perform would not be carried out. However, the argument for public methods still need to be checked.
For a private method, the programmer is responsible of ever calling it with proper arguments. This is so, because the private method is accessible only from within the class declaring them. On the contrary, public methods may be called by any other programmer using the class; and the programmer writing the class has no control over the arguments of those invocations. While debugging, assertions on arguments for private methods are checked. They are only used for debugging. They are not part of the normal operation of the program. The checks on the arguments for public methods need to be performed always however.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another reason is that assert cannot throw an exception of the type representing the specific kind of error, such as IllegalArgumentException or NumberFormatException or some user defined exception type (although it can describe the error in the detail message).
[ April 12, 2003: Message edited by: Marlene Miller ]
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is a real stupid question but here goes:
what do you mean by assert?
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
assert is a new keyword added to the Java programming language in release 1.4.
assert ref != null;
It tests a boolean expression and throws an AssertionError exception if the expression is false;
You can read about it in the guide and the spec:
http://java.sun.com/j2se/1.4.1/docs/guide/lang/assert.html
http://java.sun.com/docs/books/jls/assert-spec.html
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can expect questions about assert to be on the 1.4 exam.
See section 2:
http://suned.sun.com/US/certification/java/java_exam_objectives.html#programmer1.4
 
Charles Leung
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic