tag:blogger.com,1999:blog-11565005.post876554853463056720..comments2023-03-29T06:17:21.481-06:00Comments on The Software Life: Don't Forget to enable Java assertAndrew McKinlayhttp://www.blogger.com/profile/14951795633428513769noreply@blogger.comBlogger13125tag:blogger.com,1999:blog-11565005.post-19146689243105883132011-07-11T19:32:51.910-06:002011-07-11T19:32:51.910-06:00I think assertions are quite nice to keep around e...I think assertions are quite nice to keep around even after development. It's not until you get the end-users on the system that you really start to find the real weird bugs and without those assertions it is that much harder to figure out what is going on.<br /><br />They say "optimize late" and this is definitely a case where optimization could be more harmful than helpful.<br /><br />If you have such a "hot" piece of code that you can't afford to run the assertions, sure - optimize away those assertions. But for the 99% of code that runs once in a lifetime and is mostly I/O bound anyway, forget about it.Dobeshttps://www.blogger.com/profile/09392777569321223496noreply@blogger.comtag:blogger.com,1999:blog-11565005.post-29143923440199072262010-06-28T08:43:39.167-06:002010-06-28T08:43:39.167-06:00I'm aware of that.
What I would question is w...I'm aware of that.<br /><br />What I would question is whether it would make more sense for a development environment (e.g. Eclipse) to have asserts enabled by default.<br /><br />Even with a function and a runtime flag to enable/disable, the JVM optimization will often remove the code if it's disabled.Andrew McKinlayhttps://www.blogger.com/profile/14951795633428513769noreply@blogger.comtag:blogger.com,1999:blog-11565005.post-50952676219827627402010-06-28T02:48:22.338-06:002010-06-28T02:48:22.338-06:00Note that there's a good reason why assert is ...Note that there's a good reason why assert is a keyword and can be enabled / disabled - when disabled, the assert simply won't be executed, skipped, etc. If you replace that with an assert() function, that method will always be executed, including the expression inside it.<br /><br />So the cost of calling<br /><br />assert something == bigfunction() : "message";<br /><br />is zero, whilst<br /><br />assert(something == bigfunction(), "message");<br /><br />is the cost of executing the comparison, the bigfunction(), calling the method, and the cost of the method checking whether assertions should be handled.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-11565005.post-6222902039678508132010-05-25T04:42:39.331-06:002010-05-25T04:42:39.331-06:00If you are using Spring Test library or Junit you ...If you are using Spring Test library or Junit you can use the Assertion utility classes eg. Spring has an Assert class with many test methods on eg. Assert.notNull(myTestObject);. <br /><br />You dont need to enable assertions when using these.<br /><br />Jack<br /><a href="www.springwebdevelopment.com" rel="nofollow">www.springwebdevelopment.com</a>Tommyhttps://www.blogger.com/profile/06428056698408128873noreply@blogger.comtag:blogger.com,1999:blog-11565005.post-76578965631089737992009-10-17T13:48:23.123-06:002009-10-17T13:48:23.123-06:00Hi! Your post is very helpful! Thank you!Hi! Your post is very helpful! Thank you!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-11565005.post-55800551206009403322009-10-08T09:29:17.285-06:002009-10-08T09:29:17.285-06:00Yes, that's what I ended up doing, although on...Yes, that's what I ended up doing, although only after getting caught by this a second time. I should have thought of it the first time.<br /><br /><a href="http://thesoftwarelife.blogspot.com/2009/09/burnt-by-java-assert-again.html" rel="nofollow">Burnt by Java Assert Again</a>Andrew McKinlayhttps://www.blogger.com/profile/14951795633428513769noreply@blogger.comtag:blogger.com,1999:blog-11565005.post-75702357016202484122009-10-08T07:48:42.584-06:002009-10-08T07:48:42.584-06:00Hi I was thinking about this today....do you think...Hi I was thinking about this today....do you think it would be a good idea to write tests for the asserts to check that the exception gets thrown.<br /><br />There are levels with this.<br /><br />1. Write a test that just makes and assertion purely to check that the -ea flag has been set. Then this will fail if you forget.<br /><br />2. You could write simple unit test against each assert, expecting AssertionFailedError.<br /><br />What do you think?<br /><br />It might be a good idea to place these tests in their own package and thus be able to disable them.a.testing.timehttps://www.blogger.com/profile/16113525651105873929noreply@blogger.comtag:blogger.com,1999:blog-11565005.post-26732749429558559572009-07-08T00:14:56.996-06:002009-07-08T00:14:56.996-06:00Thx a lot for telling me this.....i was wondering ...Thx a lot for telling me this.....i was wondering the same about 'assert false' statements....Krish Perumalhttps://www.blogger.com/profile/01188906506601462923noreply@blogger.comtag:blogger.com,1999:blog-11565005.post-60594675598992700332008-09-26T23:44:00.000-06:002008-09-26T23:44:00.000-06:00Thanks! you saved me some time. It's neat that as...Thanks! you saved me some time. It's neat that asserts are toggled at runtime, and not at compile time.Byronhttps://www.blogger.com/profile/03118764718778805782noreply@blogger.comtag:blogger.com,1999:blog-11565005.post-6673384029792855492008-09-08T05:34:00.000-06:002008-09-08T05:34:00.000-06:00Thanks lot dude. you saved lot of my time..i didn'...Thanks lot dude. you saved lot of my time..<BR/>i didn't know that first we need to enable assert.<BR/>With enabling i was writing a so many programs and testing it...<BR/>Tahnks............Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-11565005.post-9296877085926284202008-08-01T04:30:00.000-06:002008-08-01T04:30:00.000-06:00Thanks mate, you just saved me from going nuts. It...Thanks mate, you just saved me from going nuts. It's a bit weird when eclipse steps through assert(false);Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-11565005.post-79398420802741602012008-05-20T16:40:00.000-06:002008-05-20T16:40:00.000-06:00I'm using JDK 6 for development but everything sho...I'm using JDK 6 for development but everything should still work with 1.5<BR/><BR/>I thought of inner classes too, but then I discovered that they are actually implemented by the Java compiler by "translating" to regular Java. The JVM itself does not have any facilities specifically for inner classes. But a similar technique could be used for blocks.<BR/><BR/>I had not really thought about EE stuff. Maybe later.Andrew McKinlayhttps://www.blogger.com/profile/14951795633428513769noreply@blogger.comtag:blogger.com,1999:blog-11565005.post-47928258822233347052008-05-20T16:20:00.000-06:002008-05-20T16:20:00.000-06:00jSuneido is a brilliant idea. Are you coding again...jSuneido is a brilliant idea. Are you coding against JDK 1.5?<BR/><BR/>The issue with blocks you may be able to solve with inner classes.<BR/><BR/>Do you plan to put the server stuff in EE container so it will run on a Java server? <BR/><BR/>I am excited to see the results!!Anonymousnoreply@blogger.com