rkm said:
This is a good example to prove my point.
No not really...
Here you have a
static method, Thread.sleep, that operates on a specific
thread instance (the current thread). There is nothing
wrong with that since it's fully documented to work that
way. However, the coder you mentioned fell under the spell
of your recommended coding convention, didn't bother to read
the documentation, and assumed calling
someOtherThreadInstance.sleep would work.
Okay, hold on a sec. I'm trying to say that Thread.sleep should be
called as "Thread.sleep(100)". You're trying to say that it's okay to
call it as "Thread.currentThread().sleep(100)". Now, consistently
seeing which of those two makes it easier to mistakenly assume that
"otherThread.sleep(100)" is also valid?
Granted, neither convention completely solves this problem by itself,
simply because of the lack of compiler checks. However, at least if you
call static methods with the normal static method call syntax, it takes
a violation of the coding convention to produce such a bug. Thank
goodness, then, that I can set Eclipse to issue a warning (or even an
error, if I wanted) when the convention is violated. Good luck doing
the same!
The coding
convention led to sloppy thinking and a bug, which, as you
put it, could be dangerous.
I think, rather, that sloppy thinking led to a violation of the coding
convention -- and a bug. The bug was easier to find because when I got
to the right part of the code, a call to sleep coded as an instance
method stuck out like a sore thumb. Better that way than sloppy
thinking leading to a bug without the convention stepping in at all.
You didn't say how you coded your fix, but if you just used
a call like Thread.sleep, then you weren't following your
recommended coding convention either. To do so, you would
have to code something like
Thread thisThread = Thread.currentThread();
thisThread.sleep(...);
Well, no. Quite the opposite, actually. Since I've been saying for
some time that "thisThread.sleep(...)" is exactly what should not
happen... I'm just confused about where you're coming from.
But most wouldn't bother with that, they would just
call Thread.sleep(...);
Which is the right way to do it.
I just don't believe I should have to understand how a class
and its instances are implemented and what their inheritance
hierarchy is in order to follow this coding convention. I
don't know how fully you intend to go with it, but if
there's some class inheritance hierarchy like A.B.C.D
and I have an instance of D called d, and I want to invoke
some static method that happens to be defined up in B, are
you suggesting I code B.methodB(), and then for some method
in A, A.methodA(), and for C, C.methodC()?
I'm confused about why it matters that you've got an instance of D
called d. That instance has nothing to do with your desire to invoke
static methods. I'm still trying to figure out why hints of instance
method usage keep creeping into a discussion of static methods, as if
there is little difference between the two.
As for the question you asked here, anything that at least looks like a
static method is preferable to something that doesn't. So you'd write
"D.method()" or "B.method()" or whatever, as long as you don't write
"d.method()". In general, most static methods naturally belong in the
class they are declared in, so it would make sense to say, for example,
DateFormat.getDateInstance instead of SimpleDateFormat.getDateInstance.
But if a programmer working for me has trouble remembering where a
static method is declared, I'd much rather the programmer still stick
with static method syntax and just use the wrong class name.
So I should
encode the class hierarchy in all my calls to static methods
(after I first research it all to see what's static and what
isn't) ??
There again, an implication that you'd know what method you want to
call, what its name and arguments are, and how it behaves, but somehow
*not* even realize something as fundamental as that it's a static
method?!? I've asked before and gotten no answer: how could you
possibly be in that situation?
Another reason I might not follow the guideline is when the
class name happens to be 38 characters long, one of which I
saw yesterday.
I suppose I can imagine that being an annoyance. I just wouldn't choose
a medicine that's worse than the illness. Luckily, in Java 1.5 the new
static import facility will remove this problem far more elegantly.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation