• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Question on Assertions

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Please help with the following question from Dan's exam.

Which statements are true?
a. Assertions should not be used to validate arguments passed to public methods.
b. Assertions should not be used to validate arguments passed to nonpublic methods.
c. Assertions should not be used within the default case of a switch statement.
d. Assertions should not be used to perform processing that is required for the normal operation of the application.
The answer given is only a,d. why not, a,b,d?
The Java documentation on Assertions says that..
Do not use assertions to check the parameters of a public method. An assert is inappropriate because the method guarantees that it will always enforce the argument checks. It must check its arguments whether or not assertions are enabled. Further, the assert construct does not throw an exception of the specified type. It can throw only an AssertionError.
My question is, even a non-public method can be called by any public method or any other sub classes. Why, assertions need not be used only with public methods but can be used with others? Can any one clarify.
Thanks and Regards,
Rama
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assertions are mainly added to test your assumptions on the block of code or method. Public methods are exposed to all the classes outside and hence you should avoid stating definite assumptions as you wouldn't be aware of the state from which the public method is called.
On the other-hand, non-public methods are accessed from other code via an object for the current class. Hence, the control for the access is passed through the class structure and you could very well state meaningful assumptions in those methods thru' assertions.
I can't get you an example to convey what I had mentioned above, anyway, itz nice enuf to understand I guess
~ Shalini
 
Rama Kumar PV
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still not clear about this. We can access both the public and non-public methods(if accessible) only through an object instance. As per the explanation by sun, using assert in public method will not allow the actual exception to be thrown(viz. NullPointerException or some other named exception). Even in non-public methods, we will encounter a similar situation. Can any one clarify about these conflicting statements on using Assertions in public/non-public methods?
Many thanks,
Rama
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The point of public and non-public here is not the Exception throwing, but rather the availability of the method to other classes and objects.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rama,
You have no control over the arguments that a client passes to a public method. You do have control over the arguments that a client passes to a non-public method because you wrote the client code. Since your own code is the client of your non-public methods, the guidelines for the use of assertions are relaxed. Even so, you should also feel free to apply more strict guidelines to your own projects if that would increase your confidence in the quality of your code. There is certainly nothing wrong with declaring a non-public method that uses the exception handling mechanisms to throw an IllegalArgumentException rather than use the assertion mechanism.
 
Rama Kumar PV
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's clear now.. Thank you very much Dan for your prompt response. Your mock exams are really great..
Regards,
Rama
 
A teeny tiny vulgar attempt to get you to buy our stuff
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic