Groups | Search | Server Info | Keyboard shortcuts | Login | Register


Groups > comp.lang.java.programmer > #4277

Re: About using assertion

Path csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!news.glorb.com!news-out.readnews.com!news-xxxfer.readnews.com!panix!not-for-mail
From tmcd@tmcd-p4-linux.austin.tx.us (Tim McDaniel)
Newsgroups comp.lang.java.programmer
Subject Re: About using assertion
Date Thu, 19 May 2011 00:32:12 -0500
Organization Tim McDaniel's home IBM-PC clone running Linux
Lines 39
Message-ID <sjmea8-067.ln1@tmcd-linux-p4.austin.tx.us> (permalink)
References <f1e4cc83-8596-4e53-bed7-34a5209fe8c4@k3g2000prl.googlegroups.com> <hp3gs6dun3691h96q13jsb6sb4pu0rrvrp@4ax.com> <iq93mt$7ob$1@news.albasani.net> <92r0e9F6lvU1@mid.individual.net>
Reply-To tmcd@panix.com
NNTP-Posting-Host 99-50-234-96.lightspeed.austtx.sbcglobal.net
X-Trace reader1.panix.com 1305785282 13499 99.50.234.96 (19 May 2011 06:08:02 GMT)
X-Complaints-To abuse@panix.com
NNTP-Posting-Date Thu, 19 May 2011 06:08:02 +0000 (UTC)
X-Newsreader trn 4.0-test76 (Apr 2, 2001)
Originator tmcd@tmcd-p4-linux.austin.tx.us (Tim McDaniel)
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:4277

Show key headers only | View raw


In article <92r0e9F6lvU1@mid.individual.net>,
Robert Klemme  <shortcutter@googlemail.com> wrote:
>On 09.05.2011 18:11, Lew wrote:
>> public class Foo
>> {
>> public double sqrt( double arg )
>> {
>> if ( arg < 0.0 )
>> {
>> throw new IllegalArgumentException(
>> "sqrt(): negative argument "+ arg );
>> }
>> assert arg >= 0.0; // precondition
>>
>> double result;
>> // rest of algorithm ...
>> return result;
>> }
>> }
>
>Lew, I am not sure what you tried to convey with this posting.  I for my 
>part would say that the assert is a tad too much here since the if 
>clause before that gives me enough "confidence" that arg is actually >= 
>0 at that line.  If it isn't then I have bigger problems than 
>calculating square roots of negative numbers. :-)

Consider

    Foo.sqrt(Double.NaN)

The "if" fails, but if assertions are enabled, the "assert" catches
it.  This might be an illustration that an assertion can be useful
even if you have "confidence" that it could never fire.

(As a tangent, I'm not sure whether "Foo.sqrt(-0.0)" should throw an
exception or not.)    

-- 
Tim McDaniel; Reply-To: tmcd@panix.com

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

#3855 
  #3861 
    #3863 
      #3864 
        #3875 
          #3881 
            #3898 
            #3908 
              #3912 
                #3916 
                #3918 
                #3925 
                #3927 
                #3931 
          #4277 
            #4279 
              #4290 
                #4294 
                #4295 
                #4298 
                #4300 
            #4292 
              #4396 
                #4399 
                #4401 
  #3862 
  #3870 
    #3871 

csiph-web