• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

When to use assertions (was meaningless: What is the logic?)

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

what is the logic behind saying that Assertions can be used to validate arguments to non-public method??
[ December 04, 2006: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a way to make sure code that you wrote, or may have some say in, does not pass an argument that will make your code blow up!

I worked on a lot of code, where we didn't use assertions, that had a ton of extra code written into the method to do things like check for 0 or null before we could use the argument. Assertions would have made that a whole lot easier and cleaner.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The statement says "Assertions are advisable to be used to validate arguments for a non-public method. which means assertions are used to validate error conditions which we are almost sure that wont occur.

To be precise, if we are almost sure an error case wont occur and we want to indicate the user when that error case occurs, Assertions are used.
Assertions throw Error which basically represents a condition that cannot be handled by the user.

A public method is always exposed to others. So, it is pretty common that invalid arguments are passed often. In those cases, it is not good to throw errors.

Instead, a non-public method is used only by ourselves. So we can add assertions with much confidence that the assertion will always pass.

This is more of a design thing than a coding idea. Also, this is a recommended thing not a mandatory thing.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Greg,

So why only to non-public methods??why not for any other methods???
 
Greg L Tonn
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Vinayagar already answered that. As Vinayagar said you don't control what a public method may or may not receive. If you did, maybe it shouldn't be a public method.
[ December 05, 2006: Message edited by: Greg L Tonn ]
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Greg,

I got the answer. Thanks for the efforts.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic