4 ms·
The Option/Maybe monad seems the way to do it. The null object pattern could also apply here, but you have to be pretty disciplined across your project to make
by mullr 14y ago
The Option/Maybe monad seems the way to do it. The null object pattern could also apply here, but you have to be pretty disciplined across your project to make it really work. Seems like the kind of thing you need language support for.
Actually: while the second code block is more readable, if I encountered it in the wild I would assume that it's full of bugs. It just doesn't seem very idiomatic. I'd much rather take advantage of the 'goto' nature of the return statement:
public Session startByInterviewId(Long interviewId, String email, String name) {
Interview interview = interviewService.getPublicById(interviewId);
if(interview == null) { return null; }
Account user = userService.createUser(name, email, interview.getLocale());
if(user == null) { return null; }
return startByInterview(interview, user);
}
There we go. 5 lines, one level of indentation for the main path, and semantics which are obvious to anybody who reads it.
But your point about the exceptions adding more information is well taken.
- deleted 14y ago[deleted]