Path: csiph.com!x330-a1.tempe.blueboxinc.net!aioe.org!feeder.news-service.com!news.albasani.net!.POSTED!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: About using assertion Date: Tue, 10 May 2011 11:21:52 -0400 Organization: albasani.net Lines: 58 Message-ID: References: <09cb7d90-9b79-473e-9869-4476c5a0191a@w24g2000yqb.googlegroups.com> <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 V2YgucvFuxfzNG3S6i7d5yyIQqS5Zda44oNiFNxwhCV6Ngi4fyaKNkJyxhehDvJTpeHRCL65tOmZ7aIlFoMfrg== NNTP-Posting-Date: Tue, 10 May 2011 15:21:53 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="TULrPQNvxVYqS0mxLQHUgjXTHym14Ej7HX9j7y0T2PTqLuhCZNsfVm5vx01a+x35p7McIHQGEQrh9DzntUbpY+kpQSnP+u2/1zPglXtFtVSudG/DoINp6f7aD3QzsAI+"; 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:CDViH8DUldDcsk7HQ/+YiH5+A/Y= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3916 On 05/10/2011 09:29 AM, Andreas Leitgeb wrote: > Arved Sandstrom wrote: >> On 11-05-09 07:49 PM, Lew wrote: >>> There is art in the decision of which invariants to document. I like to >>> document all of them. Why not? > > "All" does sound a bit harsh, doesn't it? Would you really do even: > if ( xyz != null ) { > assert xyz != null : "Something's wrong with the world..."; > ... > } > ? ;-) > >> On a related note, when I run into >> objections about use of assertions, let alone copious use of them, the >> most frequent objection is that they'll "break" the program. > > Bad assertions *can* break the program in nasty ways: > assert methodWithRelevantSideEffect()> 0 : "Aww..."; > > Another caveat is, if one uses assert reasonably but then by some > mistake assertions are unexpectedly turned off: > "We'd have got an assertion failure here, so it must have gone there ..." > > Nothing of this post shall be construed as a discouraging against > reasonable use of assertions. But they *are* part of the code, and by > adding them, code size grows, and bug-likeliness is also proportional > to code size. Adding assertions *can* be overdone. Your argument boils down to, "If you use 'assert' contrary to the instructions for its use, you'll have trouble." Assertion side effects are against all instructions for their use. You might as well say that exceptions can be "overdone" because someone might close a resource from within one. Your warning is valid, but it applies to idioms that a sensible programmer would avoid, and that tutorials and instructional articles explicitly excoriate. Code isn't "bloat" if it accomplishes something useful or even necessary. BAD code is always bloat, irrespective of how compact it may be. Your advice to avoid bad code is definitely good advice. The correlation to invariants does provide a useful rule of thumb to avoid both inappropriate and excessive applications of 'assert'. If you use 'assert' where there is no invariant, you might be screwing up. If you use 'assert' to do more than identify and prove an invariant, you might be screwing up. If you put ANY kind of program-relevant activity or domain-specific or state-changing logic into an 'assert', you might be screwing up. If you /fail/ to prove an important invariant with an 'assert', you might be screwing up. If you are unable to identify the invariants in your code, with or without an 'assert', much less enforce them (NOT with 'assert'), you more than might be screwing up, you have guaran-dang-TEED that your code ain't worth more'n' a pile of horse poop being pissed on by a junkyard dog. -- Lew Thank you, Jeff Foxworthy.