X-Received: by 10.224.189.78 with SMTP id dd14mr6769749qab.0.1360481768399; Sat, 09 Feb 2013 23:36:08 -0800 (PST) X-Received: by 10.49.15.38 with SMTP id u6mr762430qec.8.1360481768371; Sat, 09 Feb 2013 23:36:08 -0800 (PST) Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!p13no8346668qai.0!news-out.google.com!k2ni21154qap.0!nntp.google.com!p13no7103605qai.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Sat, 9 Feb 2013 23:36:08 -0800 (PST) In-Reply-To: <5116e8ec$0$288$14726298@news.sunsite.dk> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=173.164.137.214; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T NNTP-Posting-Host: 173.164.137.214 References: <8f60207e-f848-490b-a402-7cc1aba657e9@googlegroups.com> <5115d0e4$0$283$14726298@news.sunsite.dk> <5115d160$0$283$14726298@news.sunsite.dk> <21496893-281c-4f0a-803a-0466acce96f4@googlegroups.com> <5116e8ec$0$288$14726298@news.sunsite.dk> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: =?windows-1252?Q?Re=3A_compare_several_boolean_matrix=92s?= From: Lew Injection-Date: Sun, 10 Feb 2013 07:36:08 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Xref: csiph.com comp.lang.java.programmer:22261 Arne Vajh=F8j wrote: > Lew wrote: >> Arne Vajh=F8j wrote: >>> I would have done a few things differently, but ... >>> >>> public class MatrixSummarize { >>> public static boolean[][] summarize(boolean[][][] matrices) { >>> assert matrices.length % 2 =3D=3D 1 : "Number of matrices must be odd= "; >> >> Nitpicks: Some details that separate pedagogy from production. >> >> Arne's code is very good, and proper for instruction. However, if it wer= e >> to serve in production there is more to do. >> >> This is an improper use of 'assert' because there is no code to >> enforce the invariant. Properly, there should be argument-checking prior= to >> the assertion so that it represents an actual invariant. >> >> There's no check for a null argument. One could assert non-nullity after >> the check. >> >> By dropping the 'assert' in here like this, Arne has presented a secret >> challenge to fill in the gaps, as anyone schooled in the use of 'assert' >> might have noticed. >=20 > I am not that advanced. >=20 > Passing null, passing arrays with wrong dimensions etc. will all > give various exceptions. >=20 > The odd number requirement will not give an exception if violated. >=20 > So I flipped a coin (virtually) between an if throw new > RuntimeException and the assert. Assert won. It's not a coin flip. They aren't equivalent. This is well known in Java,= =20 as 'assert' has been around since Java 1.4. Assertions are more runtime comments than code. They do you no good when=20 disabled. Exceptions cannot be suppressed. Assertions are not even slightly meant for argument checking of public=20 methods. That's the job of exceptions. Assertions come in after the exception, or in private methods, to document= =20 and enforce invariants for programmers. For example, a getter method that= =20 expects the private member to be non-null can assert non-nullity. Only=20 something under complete control of the implementation, i.e., the private= =20 member, is involved, so it's time for 'assert'. When it's a public method you don't have that control. So you start with=20 a runtime check. Once that check is complete, i.e., either the method has r= eturned, thrown an exception, or continued to where supposedly you've just= =20 guaranteed non-nullity, if you are still in the method it is safe to=20 assert non-nullity. public void checkArgumentsThenAssert(Foo foo, Bar bar) { /* It doesn't make sense to assert non-nullity here because you=20 * simply do not have that guarantee. */ if (foo =3D=3D null || bar =3D=3D null)=20 { throw new IllegalArgumentException("Null argument"); } /* NOW you have the guarantee. It would take a code mistake to mess up. = */ assert foo !=3D null && bar !=3D null : "There was a code mistake"; } A quick Google will reveal the expert wisdom on the subject.=20 http://lmgtfy.com/?q=3Djava+assert http://docs.oracle.com/javase/1.4.2/docs/guide/lang/assert.html "There are also a few situations where you should not use them: "Do not use assertions for argument checking in public methods." ... "Do not use assertions to do any work that your application requires for=20 correct operation." --=20 Lew