Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assertion usage

 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per K&B, we cannot use assertions on public methods & can be used on private methods. What about methods with protected or default access specifiers ?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use assertion any where syntactically. But, assertion is used in testing purposes, so, if you used it in public methods, then it can be used by outsiders.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Abimaran..Let me modify the question. As per K&B, It is NOT APPROPRIATE to use assertions on public methods &APPROPRIATE to use on private methods. What about methods with protected or default access specifiers ?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harikrishna Gorrepati wrote:Sorry Abimaran..Let me modify the question. As per K&B, It is NOT APPROPRIATE to use assertions on public methods &APPROPRIATE to use on private methods. What about methods with protected or default access specifiers ?

Do you mean to it appropriately? Or syntactically?
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am referring "appropriately". syntactically, we can use anywhere(public, private, protected, default).
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is from K&B's book,

A public method might be called from code that you don't control (or from code you have never seen). Because public methods are part of your interface to the outside world, you're supposed to guarantee that any constraints on the arguments will be enforced by the method itself. But since assertions aren't guaranteed to actually run (they're typically disabled in a deployed application), the enforcement won't happen if assertions aren't enabled. You don't want publicly accessible code that works only conditionally, depending on whether assertions are enabled. If you need to validate public method arguments, you'll probably use exceptions to throw, say, an IllegalArgumentException if the values passed to the public method are invalid.


So, only within the private method is the appropriate, because You don't want publicly accessible code that works only conditionally, depending on whether assertions are enabled. Others are accessible from outside.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harikrishna Gorrepati wrote:What about methods with protected or default access specifiers ?


We can use as long as the guidelines/discussions related to Assertions are followed- http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.10
 
Stephan van Hulst
Bartender
Pie
Posts: 5587
53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I disagree. You can stick assertions wherever you want.

The point the book makes though, is that you shouldn't rely on them. If you want your code to fail fast and reliably, you need to use exceptions.

Assertions simply make more sense in private methods than in public, default and protected methods, because since you're the only one who has access to the method, you may assume you call the method correctly.

Personally, I rarely find assertions useful. I can imagine they are very helpful in big classes written by multiple programmers, but if I write code by myself and I can take the time to insert the assertions, I can also take the time to make sure my code works correctly.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote: I disagree. You can stick assertions wherever you want.

I agree that you can use assertions wherever possible. I thought of letting the OP know why/why not to use assert.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's important to remember the context of this thread...

For the sake of the exam (this is the SCJP forum after all), this discussion should revolve around what Sun / Oracle says about the *appropriate* use of assertions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic