2007 May 8, 15:20
Tuesday
Joshua Bloch
William Pugh
http://developers.sun.com/learning/javaoneonline/j1sessn.jsp?sessn=TS-2707&yr=2007&track=5
FindBugs - Finds bugs in Java programs
Never miss the Java Puzzlers sessions when you have a chance to see it. Joshua Bloch and William Pugh are entertaining and informative.
My objection to last year's puzzlers is that most of the problems would be avoided by good programming practice. It seemed that many of the puzzlers were based on code that no programmer of any experience would write. If there's one lesson from Java Puzzlers, it's "always employ good programming practices." If you're not sure, consult "Effective Java", by Joshua Bloch. The concepts apply to any programming language.
I'm happy to say that 2007 was different. The puzzlers were similar to code you might write or find in the wild. Many of the problems were based around auto-boxing, so always keep your code simple and perhaps verbose when using auto-boxing.
I'm not going to recount the puzzlers here. I will, however, list the maxim associated with each puzzler.
- The joy of sets: don't mix types
- Don't use URL as a set element or May key - Use URI instead. Unfortunately, URL.equals() can behave differently if you have a network connection or not. URL is based on IP, so different URLs that resolve to the same IP are considered equal!! Other lesson, don't let environment influence equals() and hashcode().
- Special code must be written for JUnit to test threads. Because the exception is thrown on another thread, JUnit will report a pass. Need another strategy to report error.
- Watch initialization order. Wrapped primitives are not primitives. Never use Boolean in a ternary operator.
- BufferedInputStream.skip() is broken. Must wrap skip() and check the return.
- Be carefule where apply absolute value. % is remainder operator, not mod operator! So, negative numbers have different remainders from moduli.
- Avoid mixing types
- Silent widening is dangerous, Use double over float.
No comments:
Post a Comment