Because many people were interested in some actual code, here is an example that we used for Java:
---
public class UserDao {
private Provider<Session> sessionProvider;
public UserDao() {
this.sessionProvider = new DbSessionProvider();
}
public boolean saveUser(User user, ApiConfig config) {
try {
Session session = sessionProvider.get();
session.save(user);
System.out.println("User saved!");
return true;
} catch (DbException e) {
EmailUtil.sendEmail(config.getTechnicalSupportEmail(), e);
return false;
}
}
}---
"Solution": We always ask the candidate "What would you change about this code?" or something similar. We expect the candidate to come up with some selection of: - Database sessions should come an injected provider. - Configuration should probably be injected as well and not passed as a method parameter. - Booleans are not canonical Java as return value. - Don't write to stdout but to a logger. - Exception handling should probably happen outside the DAO. - Don't use static methods for sending Emails without good reason. Non-static methods are easier to test.
Finding these things is as important as being able to talk about them and give background on advantages / disadvantages of doing things different ways. With good candidates one can talk easily half an hour just about this example and adjacent topics (e.g. error handling).
---
Edit: Formatting
Edit 2: added "Solution"