Hacker News new | ask | show | jobs
by vaylian 666 days ago
> Booleans are not canonical Java as return value

What should you return instead? Or should the method return void and raise an exception on failure?

2 comments

You could return the saved User object, if `save` changes it in any way, to allow the caller to work with it functional style, ie. make it explicit that it is an updated object (or if it is immutable, although typical persistence frameworks expect mutable objects, "bean" style).

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...

Probably boolean. If you don't see the difference, welcome to Java :D
Ah, but the code already uses boolean instead of Boolean.
Whoops, so it does. In which case I have no idea what the solution is referring to.
No serious Java code uses booleans to indicate failure or success. It's on the same level as not using a language's standard variable naming scheme. It's not wrong, but something experienced Java developers working on good code will immediately notice.

In good Java code you would return either void to indicate side effects or the newly saved user. Exception handling should be done outside the function, because depending on the context you might retry etc.

The main idea is to check if people who say they are very experienced understand conventions and have experience with common Java libraries.