(no title)
xgb84j | 1 year ago
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"
normie3000|1 year ago
Izkata|1 year ago
It's not a bug ("this" is implied, so it works just fine), but leaving it out hints to me that it was originally a third argument to "saveUser()", and either the original person writing this didn't have a solid API in mind or "saveUser()" was intended to also be static like "EmailUtil.sendEmail()" and it was switched around later on. Overall hints towards two conflicting styles in the same codebase and no good guidelines. Or maybe there are such guidelines (as hinted in the "solution"), and whoever updated this class only did it partway. This is the point where I'd poke around in the repo history to see why it's like this and whether anything should be changed further in either direction.
bilekas|1 year ago
Sr.Dev : "UserSignup failed, ask tech support what happened in our code"
Genius.
actionfromafar|1 year ago
vaylian|1 year ago
What should you return instead? Or should the method return void and raise an exception on failure?
haspok|1 year ago
You could also go fancy and do it properly functional, so return something like an `Either<User, Error>` instead of throwing an Exception, but that's definitely not canonical Java...
normie3000|1 year ago
hkon|1 year ago
whatever1|1 year ago
Not great advice given the fact that a logger paged pretty much every SDE on this planet.
nailer|1 year ago
That said, modern Linux OSs send stdout to journald by default. journald should forward to some centralized logging server.