Re: Is creating anonymous objects bad practice?



Tom Anderson wrote:
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.

I agree. One of the risks of putting too much into a constructor is that the actions are performed by an incompletely-constructed object, as an exemplar of the engineering decision that goes into that. It is meant as a distinction from maintainer confusion or expectation. It is certainly not the only problem with doing such things.

--
Lew
.