Trick or Bleep?
I like to post fun stuff on Fridays. I also repost old stuff from time to time when my brain is page faulting or when something I’m reading reminds me of an old post. Today both were true: I spotted
this post about interviews going wrong and I remembered
an interview where I was the hapless victim.
There has been some refreshing commentary on the post and also on
reddit. I want to give my thoughts on one issue. Was the interviewer posing a “trick question,” trying to find out how I negotiate conflict? This subject has attracted a lot of attention since Joel Spolsky recently
raised the issue, and while it didn’t cross my mind at the time, it’s worth considering.
The question posed to me was,
list all of the ways to call instance and class methods in Java. I listed a bunch of things, and at the end, I included:
someInstanceVariable.staticMethod();
// calls the staticMethod of someInstancevariable's declared class
The interviewer told me I was wrong, and I shrugged, explaining that I thought I was right, but no big deal if I was wrong, since I never use that style of call, so it wasn’t an issue. The interviewer later told me that he did not like my answer, and indeed I did not get an offer.
If this was a trick question,
ok, you had me! I am not afraid to argue points where I believe there is a direct connection between the issue and the outcome of software development projects. If I really believe it is irrelevant, I don’t argue even if I have a personal preference. So I’ll happily go along with any particular coding style you advocate (including none), and if you insist that I use Eclipse for Java instead of TextMate, fine.
The problem with that question (let’s be clear, it’s
my problem, not the interviewer’s problem), is that I don’t really care whether Java lets you call a static method via an instance of a class. The reason I don’t care, as I explained to the interviewer, is that I will
never allow that code to get into the repository.
For those who are too busy with Lisp to know all of Java’s warts, the problem is that static or class methods are never polymorphic. So if you write:
// (yes, I know about public vs. private. These are obviously in 'package scope')
class House {
static String doorAction () { return "closes and locks"; }
}
class S___House extends House {
static String doorAction () { return "bangs"; }
}
and you do this:
final House structure = new S___House();
System.out.println(structure.doorAction());
You get
closes and locks
instead of
bangs
. Always. This is
not what happens with instance methods, and that’s because so-called ‘static methods’ in Java are really global functions that live in class namespaces. The compiler resolves the function
doorAction
by looking up the declared type of
structure
at compile time. Its actual type at runtime is irrelevant. C++ programmers recognize this as the difference between
virtual and non-virtual functions.
Back to the “trick question.” My problem is that since I never have and never will use this, I’m not going to argue whether it’s legal or not. It will never have the slightest impact on any project I’m on. If you want an argument, let’s discuss why you think we should use this idiom in production code.
I honestly don’t think it was a trick question. If the interviewer knew it was legal and wanted an argument, he could have (and I suggest should have) chosen a different thing to argue, something a little more obvious, such as telling me it is
legal and polymorphic, and then seeing whether I (a) know that it is not polymorphic, and (b) argue with him.
I don’t use questions like this, but I can see the value of this second question: a really experienced person knows exactly why it isn’t polymorphic and can discuss the kinds of bugs you will get if you try to write code depending on it.
What do you think? Was this really a trick question? If so, should I have been more aggressive in arguing the subtle points of Java law?
p.s.
The interviewer later told me that he did not like my answer, and indeed I did not get an offer: one benefit of much water having passed under the bridge is a more balanced perspective. I no longer assume that this one question is the only reason the interviewer decided I was not appropriate.
“Blink” examines why people make unconscious decisions. It's very relevant to interviews!I may have been overqualified, too talkative, too reflective, or even too meticulous (when given the programming challenge of permuting a string, I was apparently the only candidate to ask what the output should be when the string contained duplicate letters).
Perhaps the interviewer had already decided the interview outcome before this question was discussed.
And do you know what? I’m not so sure I wouldn’t have reacted the same way as the interviewer. One of the big “No Hire” flags for me is a candidate who doesn’t fundamentally “get” a programming language. Anyone can “Learn Blub in 21 Days,” memorizing code snippets and patterns by rote. But such candidates often have no clue how a language works, how its features interact with each other to make those memorized code snippets work.
Let’s say that I’m interviewing and I don’t know that Java (which isn’t particularly OO in theory or in practice) allows this bug of calling a static ‘method’ via an instance of a class. Is that so bad? Of course not, it’s a wart on the language. It’s not like knowing that it is allowed reveals a deep understanding of how Java works. So not knowing this isn’t a fundamental flaw in a Java developer or interviewer.
Now a candidate tells me that this thing works. Well, I might jump to the conclusion that the candidate really doesn’t get the difference between static and instance methods, that the candidate will have trouble understanding the various scoping issues involved with instance members and static members. I could easily believe that this candidate will draw a complete blank when working with inner classes.
An interviewer who didn’t happen to know this bit of Java trivia might reasonably infer that thinking it works betrays a superficial knowledge of Java.
Labels: java, jobs