Re: Style Police (a rant)
On Saturday, August 27, 2011 4:59:53 PM UTC-7, Jan Burse wrote:
Lew schrieb:
[... snip ...]
Looks like you are preaching to the convert.
Anyway, here is some fun:
class A {
A() {
init();
}
init() {
}
}
class B {
foo = 3;
init() {
super.init();
System.out.println("foo="+foo);
}
}
What value for foo will be printed when I do
new B()?
Would it make sense to put a final on init()?
Given that calling non-final methods from a constructor is a very well-know=
n antipattern, it's a bit like asking, "Should I drive on the wrong side of=
the road?" You can get away with it for a while, perhaps, but sooner or l=
ater you're going to run into trouble.
This is an aspect of the rules I've been repeatedly citing in this thread: =
"Item 17: Design and document for inheritance or else prohibit it", "use of=
'final' on classes and methods is a semantic restriction ... frequently th=
e right thing to do", "[s]uch restrictions increase the safety and predicta=
bility of the code and systems built with it", "you have to be responsible =
for the consequences to program logic". Thanks for illustrating the points=
..
--
Lew