• 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
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

why ?A assertion question.

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assertions should not be used to validate arguments passed to public methods.
but,
Assertions can be used to validate arguments passed to nonpublic methods.
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy -- great question!
The idea that it is "not appropriate" to validate arguments to a public method (as opposed to it being "appropriate" to validate args to a private method) is because with a *public* method, you're exposing that method to the outside world, so you had better be taking care of the validation in your code (including use of exceptions if necessary).
In other words, if the method is public, then code *you* did not write and cannot control can invoke that method, so you must protect it with programmatic validation. But... if it is a *private* method, then *you* are the only one who is writing the code that invokes that private method, so it's OK to use assertions to test what you are assuming that *you* have sent in.
So that's the main difference. Remember that both are legal (can compile and run), but the exam expects you to know what is and is not an "appropriate" use of assertions, and this is one example (public vs. private method arg validation).
Cheers,
Kathy
(working on becoming more assertive )
 
Mike Lin
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks! :-)
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic