This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Micro Frontends in Action and have Michael Geers on-line!
See this thread for details.
Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Improper Assertion

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Refer to Dan's Beta Mock Exam (Assertion):
When will we say the using of assert statement is proper or improper statement ? And why if we use assert to validate the argument of public method is an improper use, altough there's no error in the result ?
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Refer to Dan's Beta Mock Exam (Assertion):
When will we say the using of assert statement is proper or improper statement ? And why if we use assert to validate the argument of public method is an improper use, altough there's no error in the result ?


Because primary purpose of assert construct is to aide in the program development. In the delivered code, it should not be doing any critical steps. That will not be good use as of assert.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose I write a method public void m(int n){} where I insist that 0 < n < 10. If I use an assertion assert ((0 < n) && (n < 10)); this will throw an AssertionError if n <=0 or 10 <= n.
But it will only do that if assertions are always enabled at compiletime and runtime.
If I wish to protect my method at all times I must do something like if (!((0 < n) && (n < 10))) throw new IllegalArgumentException();. This will work all the time and does not depend on the assertion mechanism.
There is also the form of contract where one says
"my behaviour is unpredictable if my precondition is not satisfied" which puts the responsibility on the caller to provide argument checking. In this case assertions could validly be enabled for the testing phase and disabled in production.
-Barry
[ September 20, 2002: Message edited by: Barry Gaunt ]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The current issue of the JavaRanch newsletter has two articles on assertions:
http://www.javaranch.com/newsletter.jsp
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Denny Permana:
Refer to Dan's Beta Mock Exam (Assertion):
When will we say the using of assert statement is proper or improper statement ? And why if we use assert to validate the argument of public method is an improper use, altough there's no error in the result ?


Denny,
Thank you for trying my exam.
Thank you Barkat and Barry for your responses.
The exam objectives state the following: "Write code that makes proper use of assertions, and distinguish appropriate from inappropriate uses of assertions".
Sun provides descriptions of appropriate and inappropriate uses of assertions in the paper titled: "
Programming with Assertions".
The following is a quote.


Do not use assertions for argument checking in public methods.
Argument checking is typically part of the published specifications (or contract) of a method, and these specifications must be obeyed whether assertions are enabled or disabled. Another problem with using assertions for argument checking is that erroneous arguments should result in an appropriate runtime exception (such as IllegalArgumentException, IndexOutOfBoundsException, or NullPointerException). An assertion failure will not throw an appropriate exception.

 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic