My Favorite Interview Question

Recently I have been doing some job interviews (as an interviewer, not a candidate). Although I hate being schematic and try to come up with new & interesting questions for every candidate, I always ask My Favorite Interview Question, which is:

What are the benefits of using Dependency Injection pattern?

The answer for that immediately tells me, whether the candidate has broader horizons or is just a doing-what-I’ve-been-told-to-do drone.

So, what’s a bad answer? Anything along the lines of “DI allows you to configure your dependencies and inject them by a framework.” Seriously, that’s an answer that I would come up with if I had not even the faintest idea what DI is, and I get it from developers who claim several years of experience, working with Enterprise Java and all that stuff.

Even worse: candidate mentions XML in the first sentence. I mean, what are they thinking? Is “using XML” among the benefits of Dependency Injection? (I would say it’s a cost, not a benefit, but let’s leave that for another rant.) My point is, DI and XML are concepts from totally different and unconnected levels.

When you use DI, you might want to have your dependencies automatically injected, for that you might want to use the Spring framework (if it’s a Java project), and then you might want to configure it with XML files. See how many steps it takes to connect DI and XML? On each step you could decide to use something else, so there’s a good chance that you end up with no XML at all. And yet, XML has been imprinted so deep in Java developers’ brains that you might hear “DI lets you configure your classes with XML.”

OK, so what qualifies as a good answer (at least according to me)? Anything from “DI improves testability because you can easily mock dependencies” to “DI enables loose coupling” to “DI enables flexibility” to “I don’t see any benefits of DI, therefore I don’t use it” even.

In fact, any answer in form of “I don’t see benefits of X, therefore I don’t use X” is better than “I use X because they told me to/everyone uses it/that’s how it was always done.” It shows that you made a conscious decision about using or not using X. Of course, I would still like to know why you don’t want improved testability or loose coupling. But at least this is something that can be further discussed.

And the best answer would be that you don’t use DI, DI just happens when your code is testable and loosely coupled. So, if you happen to be interviewed by me, you will be asked this question, too. Now you probably know what answer would satisfy me. And if you get the job, that would probably be the first case of someone benefiting from reading this blog :)

One response to “My Favorite Interview Question

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: