• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Assertion usage

 
Ranch Hand
Posts: 423
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am referring "appropriately". syntactically, we can use anywhere(public, private, protected, default).
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 13891
314
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 9034
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Power corrupts. Absolute power xxxxxxxxxxxxxxxx is kinda neat.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic