File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why not assertion for public methods ??

 
Geet Chaudhary
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a doubt .. why can't we use assertion for public methods ???
I have read somewhere "An assert is inappropriate in public methods because the method guarantees that it will always enforce the argument checks. A public method 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".

So, wont it applicable for private method too ??
I didnt understand the above statement clearly ..
 
Stephan van Hulst
Bartender
Pie
Posts: 4838
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you. Personally I think assertions are pretty useless, but a lot here would disagree with me.

The point is, it's perfectly fine for you to add assertions in a public method, but you should not rely on them, and you should do actual precondition checking with proper exceptions.

The reason they are used only in private methods, is because you don't have to do precondition checking in private methods. Why? Because you wrote the code by yourself, and you may assume that you always adhere to the contract of your private methods. Assertions are used as a kind of backup check, just in case.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic