• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

When should I use assertion ?

 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
The following code is copied from the document in java.sun.com
<code>
if (i % 3 == 0) {
...
} else if (i % 3 == 1) {
...
} else { // We know (i % 3 == 2)
...
}
</code>
and it mentions that use an assertion whenever you would have written a comment that asserts an invariant. And recommand to use assertion like the following.
<code>
if (i % 3 == 0) {
...
} else if (i % 3 == 1) {
...
} else {
assert i % 3 == 2 : i;
...
}
</code>
Could anyone tell me why the above usage of assertion is appropriate ?
What is the purpose of "assert i % 3 == 2 : i;" statement ?
Thanks in advance!!
Jack
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



What is the purpose of "assert i % 3 == 2 : i;" statement ?


The program only expects 3 values of i: 3 (i%3==0), 4 (i%3==1), & 5 (i%3==2). Any other values should not exist.
So what the program is saying is, if the 1st two conditions are not satisfied, then i must be 5. And the assert statement makes sure that it is before it continues with the rest of the code.
It is like writing the program like this:
 
Jack Lau
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh, i see. Then it must be used only for testing because it will make the program terminate, am I right ?
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jack Lau:
oh, i see. Then it must be used only for testing because it will make the program terminate, am I right ?



Not just terminate but also might not behave properly.
But yeah, you use it to improve the reliability of your code. If you are familiar with C's assert macro, it is very similar.
 
Jack Lau
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much!
Jack
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


What is the purpose of "assert i % 3 == 2 : i;" statement ?


IMHO the purpose is to replace a simple comment with a sentence that both serves as a comment and it checks certain stuff.
__________________________________________________
The program flow should not depend on the points tested by assertions, because assertions are not enabled in production time. The usefulness of assertions are to check the programmer assumptions during debugging, and to pinpoint problems at production time when the user is instructed to enable assertions.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The programmer is assuming the value of i will always be a multiple of 3 (0, 3, 6, 9, 12 ...) or a multiple of 3 + 1 (1, 4, 7, 10, 13 ...).
The programmer knows there is a bug in the program if i is a multiple of 3 + 2 (2, 5, 8, 11, 14 ...).
The programmer ignores the case where i < 0.
For example i = (x * x) * 3 + (x * x) % 2;
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic