Is creating anonymous objects bad practice?

L

Lew

Arved said:
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.

Software decisions are justifiable on an engineering basis, not on what people
expect or what will confuse them. It's a matter of the code doing what it is
supposed to instead of failing.
 
A

Arved Sandstrom

Eric Sosman said:
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?

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
 
A

Arved Sandstrom

Lew said:
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.

Software decisions are justifiable on an engineering basis, not on what
people expect or what will confuse them. It's a matter of the code doing
what it is supposed to instead of failing.

Ok, maybe it's just me. Because when I look at code - mine or that of
others - I sure do *expect* certain things to behave a certain way.
Including constructors. The _reason_ I expect that is because of software
engineering best practices or local conventions.

AHS
 
T

Tom Anderson

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
 
L

Lew

Tom said:
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.
 
A

Arved Sandstrom

Lew said:
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.

And I believe we are on the same sheet of music. When I said that I (or
anyone else) expect a constructor to only initialize an object, that
expectation exists because of sound OOP principles. However, there's no
question that if those (over any other) principles are not followed,
confusion results...among other things.

AHS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top