This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
You can use it to execute code that must be run in all constructors without needing to copy that extra code. You can use a private method for that but you may forget to add the call to that method if you add a constructor.
Are there any style/usage guidelines for doing this? I used to get yelled at because it wasn't "clear" what was happening. My response was "then you need better Java programmers that actually know the language", but that went over as well as you'd expect. And I refused to put a comment above it saying what it was or when it ran >:|
Generally I think initializer blocks are obscure enough to avoid in most code. They're handy for initializing anonymous classes and in a few other special cases, but I'd tend to agree that there are clearer alternatives in most cases. Unit testing avoids Rob's drawback to common init methods.
But... Java is restrictive enough; I'm really, *really* loathe to avoid what few features it has. Things are obscure only because they're not used, and I'm uncomfortable using that as a reason to avoid standard functionality.
David Newton wrote:I used to get yelled at because it wasn't "clear" what was happening. My response was "then you need better Java programmers that actually know the language", but that went over as well as you'd expect.
Peter Taucher wrote:
David Newton wrote:And I refused to put a comment above it saying what it was or when it ran >:|
If someone is asking me to dumb down my code because others don't know the language well enough (or sometimes aren't smart enough) to read it, then stuff 'em! Why should I adapt when making the others adapt would be much better in every aspect? They get to be smarter, my code stays as smart as it was, and most importantly, I don't have to do any extra work
Joined: Nov 18, 2006
I can follow your arguments ... BUT ; - ) a little comment here and there for the less professional developers in your team may not hurt that much.