Infamous programming errors on curly braced {blocks}
0 Comments Published June 11th, 2005 in Horror Code, Java Tags: .It was a late evening when I fall in buggy a code like this:
Destination newDestination = null; if (! originalDestination.getId().equals(getDestinationId())); { newDestination = context.loadDestination(getDestinationId()); prefix.setDestination(newDestination); } context.save(prefix); if (newDestination != null) { newDestination.addPrefix(prefix); originalDestination.removePrefix(prefix); context.save(originalDestination); context.save(newDestination); }
When the first if block is true, the second one will also be true, because the line at 4 will force newDestinatio to be not-null. The strange thing was that the second if-block was always executed, so I started to do step-by-step debugging. Working with cglib-proxied objects (hibernate) I was starting to think on some bug somewhere in the framework. Wrong! The bug is there and was under my nose but nicely hidden. Can you see it?
Check the end of line 2, at the end of the if condition... there's a ";" so that the first if will execute an empty statement, and the block under the if will ever been executed. It was so surprising checking internal values thru the debugger and then see the if being executed even if the condition was false!
I was surprised tha Eclipse does not pointed me over the code inconsistency (a warning). If you enable warning on empty statements (Preferences->Java->Compiler->Errors/Warning->Potential Programming Problems->Empty Statements), you could notice easier errors like this. But there's no way to define a warning of blocks {} without a previous construct (if/while/for...). Even if sometime correctly used, empty blocks are always ugly (i.e. for(int i=0; i>x; doSomething(i++)); ugly!). So I enabled this as an error, in eclipse compiler. Would be nice to be warned on instruction blocks without a construct on top (like the range of lines between 3 and 6).
Search
Archives
Categories
- Android (3)
- Apple (26)
- Books (7)
- Eclipse (14)
- Errors (3)
- Firefox (7)
- Git (2)
- Hardware (16)
- Horror Code (8)
- Internet (18)
- Java (98)
- JavaScript (9)
- Life, universe and everything (45)
- Lifehacks (25)
- Linux (50)
- Opinions (25)
- OSX (4)
- Python (1)
- Software (27)
- Speeches and Conferences (8)
- Unix (3)
- Web (21)
- Windows (19)
Tag Cloud
Android apple architecture Bash colors configuration CSS Development Düsseldorf Eclipse germany Git Google Hardware hdr How-To Java JAXB job junit Karmic Linux MacBook music night Open Source Opinion oracle OSX patterns Pitfalls Practices Resume Security Software Suspend TDD Testing tip tonemapped Tricks Ubuntu video Web XML
WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.
Blog License
Blogs I like
Books on the desk
Friends' Blogs
- Antonio Terreno & Valter Bernardini
- Bruno Bossola
- Daniele Galluccio
- Domenico Ventura
- Ed Schepis
- Fabrizio Gianneschi
- Luca Grulla
- Luigi Zanderighi
- Marcello Teodori
- Mida Boghetich
- Muralidharan Chandrasekaran
- Piero Ricca
- Renzo Borgatti
- Simone Bordet
- Simone Bruno
- Uberto Barbini
- Valvolog
- Webtide blogs (Greg Wilkins & Jan Bartel)
Links




















No Responses to “Infamous programming errors on curly braced {blocks}”
Please Wait
Leave a Reply