Re: Is creating anonymous objects bad practice?
- From: Tom Anderson <twic@xxxxxxxxxxxxxxx>
- Date: Sat, 29 Nov 2008 19:25:49 +0000
On Fri, 28 Nov 2008, Lew wrote:
Arved Sandstrom wrote:To put it very succinctly, people expect constructors to return an initialized object. And that's all a constructor should do. It will confuse other people reading your code if you start non-initialization in a constructor. In your above example a maintenance programmer would figure it out, but it's not always that obvious.
It's got nothing to do with what "people expect". Doing work with an incompletely-constructed object leads to bugs, that's why it's bad.
Hold on, hold on, the issue in this thread isn't about incompletely constructing objects, it's about the other fork of Arved's succinct putting - that constructors should not do anything else. Consider:
public class LogMessage {
public LogMessage(String msg) {
System.err.println(msg) ;
}
}
Which is used OP-style like this:
doSomething() ;
new LogMessage("did something") ;
There's no risk of incomplete construction here, but i think we'd all agree that this is bad style nonetheless.
tom
--
This is your life and it's ending one minute at a time.
.
- Follow-Ups:
- References:
- Is creating anonymous objects bad practice?
- From: Peter D.
- Re: Is creating anonymous objects bad practice?
- From: Arved Sandstrom
- Re: Is creating anonymous objects bad practice?
- From: Lew
- Is creating anonymous objects bad practice?
- Prev by Date: Re: Oddity with java.util.SortedMap
- Next by Date: Re: Simple integer factorization algorithm
- Previous by thread: Re: Is creating anonymous objects bad practice?
- Next by thread: Re: Is creating anonymous objects bad practice?
- Index(es):
Relevant Pages
|