X
xarax
Alex Hunsley said:Thomas said:Alex Hunsley coughed up:
...[rip]...
Overly strict. I cannot /count/ the number of times I've seen
horribly convoluted code set out to avoid putting nearly /anything/
into a constructor.
"Constructors should *not* call non-final or
non-private methods" is not overly strict
STOP RIGHT THERE.
Please don't shout, it's considered rude.
You meant: should *not* call non-final or non-private methods of that same
object? Sorry, I misunderstood your intent.
well, if I didn't mean that, then I would be talking about calling
private methods of *other classes* which wouldn't make any sense. So
yes, I meant of that same object, sorry if I wasn't clear on that.
Then I totally agree.
Here's something else I see *all the time*: (pseudo-java, ignore scoping
rules):
class A
{
A() { helper(); }
helper() {....};
}
class B extends A
{
helper() {....}; // dangerous: overridden
}
Yup, I see that too. I see dead people.
Yes, that can be dangerous when the subclass
doesn't properly handle the set-up that the
original helper() is supposed to do.
OTOH, I've seen things like this:
public class A
{
private final List myList;
protected abstract List init();
protected A()
{
myList = init();
}
}
public final class B
extends A
{
protected List init()
{
List result;
result = /* Create and initialize the List */;
return result;
}
}
The c'tor for class A is designed to call
method init() that is implemented by a subclass.
This is a good design, because there is no
"default" version of init() to override.
The subclass must fulfill its implied contract
with the parent class, and it must be careful
not to reference any subclass elements that
have not yet been initialized (because the
parent is still constructing). The init()
method must be completing self-contained with
no dependencies on the subclass instance.