Re: Is creating anonymous objects bad practice?



"Eric Sosman" <esosman@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:ggqca0$v8c$1@xxxxxxxxxxxxxxxxxxxxxx
Andreas Leitgeb wrote:
Arved Sandstrom <asandstrom@xxxxxxxxxxxxx> wrote:
new LogThis().log("Log this string");

Unless the log-method really really needs an instance
of its class, this is almost as bad as the constructor
thingie. "Almost" (and not "entirely"), because at least
we do not use incomplete objects that way.

Just make log static and do Logthis.log(...);

Well, there's the possibility that Log might have more
than one constructor:

new LogThis().log("Log this string");
new LogThis("foo.log").log("Log this string");
new LogThis("foo.log", LogThis.PRIORITY_MEDIUM)
.log("Log this string");

Even so, though, it looks pretty silly to (presumably)
create/open, write to, and flush/close a log in the
LogThis constructor. Makes retaining a LogThis instance
sort of useless, no?

--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxxx

Arguably logging is a bad example anyway. We are reading too much into it.
Namely, the points you made about what that LogThis ctor would have to do,
unless the log() method did all of it, which is also ungood.

AHS


.



Relevant Pages