This week's giveaway is in the Spring forum.
We're giving away four copies of Microservices Testing (Live Project) and have Chris Love & Andres Sacco on-line!
See this thread for details.
Win a copy of Microservices Testing (Live Project) this week in the Spring 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

inappropriate use of assertions

 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, i came accross the following question.


Which fragment is an example of inappropriate use of assertions?
A. assert(!(map.contains(x)));
map.add(x);
B.if (x>0) {} else (assert(x==0);}
C.public void aMethod(int x) { assert(x>0);}
D.assert(amethod2());
return retval;
E.switch(x) {
case 1: break;
case 2:break;
default:assert(x==0);


I thought the answer should be C as we are creating a method just for the purpose of asserting.
Am i thinking in the right direction? or any other answers?
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think C, because we shouldn't use assertion to check parameters of a public method, and D, because amethod() is a side-effect and won't be called with asserts disabled.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
D could just be a get method and not do anything else, so just C.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Adrian, think the C is the way to go - assertions should not be used in the public methods, one should rather have all the logics built INSIDE the method because you can never tell who will use your method and with what parameter values.
OTOH, I'm not sure about the D, the first part of the assert should be an expression that evaluates to true or false, so if the amethod2() method returns boolean, it's OK, otherwise I'd put it on the list as well.
[ March 05, 2004: Message edited by: Bojan Knezovic ]
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
that's your code ..

--------------------------------------------------------------------------------
Which fragment is an example of inappropriate use of assertions?
A. assert(!(map.contains(x)));
map.add(x);
B. if (x>0) {} else (assert(x==0);}
C. public void aMethod(int x) { assert(x>0);}
D. assert(amethod2());
return retval;
E. switch(x) {
case 1: break;
case 2:break;
default:assert(x==0);
--------------------------------------------------------------------------------

I think neither C nor D is appropriate use of assertions, but is that all?
No! A is using a method call to check assertion condition, it's not appropriate use specially if that method will affect any value! it depends on the method code!
ain't?
Fethi
 
Adrian Pang
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's ok to use the return value of a method for assertion, as long as the correct operation of the program does not depend on invoking the method. Since it's unlikely that the program in A will depend on .contains being called, I think it's ok.
By the same token, as long as the correct operation of the program in D doesn't depend on amethod() being called, it's ok. =)
-- Exam in an hr, getting nervous =)
Adrian
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic