Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: About using assertion Date: Thu, 19 May 2011 09:38:41 -0400 Organization: albasani.net Lines: 64 Message-ID: References: <92r0e9F6lvU1@mid.individual.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net W6Pyz8DPSiSUP61EKThx9WchbJ2IAvYbv5WK8kuDqrAhNZ9NM2CJ0FSiqs5+blMqZ09pwx/KWubiWQSXOraqpNhSNmyepp5E+x7t7L93d1CNK6mvpoksQL3pTY9pAB1L NNTP-Posting-Date: Thu, 19 May 2011 13:38:13 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="vNyhL9OfA07rVfXExFt5IJDQLxWYnGqUzg6TzbZv+PuxocWJmd0ewRoaJ6R/XWh8r6OjS4ui6Gf2l+sbFdVSTC9qDIn7v/a5lnVAAT8qLYWdr9ymnXKBufGOEU73Vlgx"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.17) Gecko/20110424 Thunderbird/3.1.10 In-Reply-To: Cancel-Lock: sha1:UV9RlP6tZA2FLxgdBRpGk+2Up64= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:4295 On 05/19/2011 09:16 AM, Michal Kleczek wrote: > Lew wrote: > >> >> That is wrong. That needs to be checked with an 'if' if 'NaN' is not a >> legitimate input. 'assert' is not meant to correct things, only to make >> sure > > I totally agree with you. > >> that the correction is documented and proven. If 'assert' is turned off >> in production, you still have the bug if you use 'assert' as the only >> guard. > > I was imprecise - just wanted to praise an example of "having confidence" > being sooo wrong and how having assertions (even though they look redundant) > _may_ help you. Totally correct. Assertions are a signpost: HERE THERE BE AN INVARIANT! When subtypes or refactored code violate the invariant, the assertion can help catch it. public class Foo { private Attribute attribute; public void setAttribute( Attribute attr ) { if ( attr == null ) { throw new IllegalArgumentException( "null argument, dummy" ); } attribute = attr; assert attribute != null; } public Attribute getAttribute() { assert attribute != null; return attribute; } } public class Bar extends Foo() { public void setAttribute( Attribute attr ) { attribute = attr; } } The assertion will help reveal that class 'Bar' violated the invariant. In production, the 'NullPointerException' (in the logs from 'getAttribute()' clients) will trigger an investigation, which will include enabling assertions for the class that yields the value, and then the invariant checks will help find the problem. -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg