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 ]
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
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.
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.
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 ]