The Optional is an awesome resource to be used today in modern Java. Just because you "can" use it in param, it doesn't mean that you should and in case you have it somewhere around... red flag! Each language has its own idioms, and Java has clear conventions around how features like Optional should be used. 🔗 https://lnkd.in/eMYGEth2 #java #optional #cleancode
Optionals in Java are awesome! However, passing Optional objects as arguments is generally not considered a good practice. Optional was primarily designed to act as a return type to prevent NullPointerExceptions. Besides, passing it as an argument doesn't eliminate the risk of receiving a null value. So, why would you do it? Keep in mind that even though Java and Kotlin might look very similar, they are entirely different languages. Optional<Token> parseToken(String token) { if (token == null || token.isBlank()) { return Optional.empty(); } try { return Optional.ofNullable(parser.parse(token, Token.class)); } catch (SpecificParserException ex) { // Even catching the Exception class isn't a good practice; try to catch a specific exception whenever you can. return Optional.empty(); } }
Is it that awesome though? I mean compared to ?. And yes it is not recommended to be used as params, it adds more noise to the code.
Omg such an stupid code This whole function can be done In one line of code much more readable
but kotlin is awesome :P
using Optional as input yet ignoring the core idea and still returning null, hat down..
My gosh. They invented optional so you don't use null. Then someone says : don't use on parameter list. That's the problem with java. The developers. You could simply have optional.map(token -> parser.parse(token, Token.class).orElseThrow("Error while parsing"). Optional exists since v 8, 12 years, and you still use it like if it was java 1.5
To use Optional as a parameter, it's necessary to change the whole approach and stop using null entirely. Then Optional from noise transforms into an explicit contract that tells the user that the parameter can be missing. So, in the legacy imperative coding style, Optional as a parameter (or field) is rather an antipattern. But in the modern functional style, this is the way to express the possibility of the value to be missing.
Thank you In my understanding passing an Optional as a parameter is a bad practice/smell code I think the intent of this is to reduce NPE and make the validation before calling the method. Passing it as a parameter breaks this idea and keep in mind that it’s an Object. What if another method in the same methods needs the parameter, we should pass it as Optional as well I’m saying this because I am in a Scala project and we use Optional all the time but never see it as a Optional parameter
It depends. One case that you might benefit from using Optionals is when you actually treat them as a monad. You can then describe the computation as a chain of operations (like with streams). A map, flatmap or other operation will just continue forward as an empty optional. It makes them safer than unwrapped objects. Imperative code without null checks would just throw if the object was null. By using Optional this way you can reduce the boilerplate and leave only what matters. If your code is already using streams in other places, it's worth considering using Optionals in a similar way.
Hello Bruno Andrade, I often see people wasting a lot of the potential of Optional in Java. When I first saw it, I immediately thought about all the times I had methods that needed to return something — and all the other methods that depended on that return value. That was a problem because the validations and error-handling decisions inside one method ended up forcing behavior onto others. I constantly had to decide whether to return null or throw an exception when a value wasn’t found, and whoever called that method had to adapt to that specific behavior. Optional came to fix exactly that by properly separating responsibilities. You return an Optional, and whoever receives it decides how to handle the absence of a value. I love that. What do you consider the most powerful Optional function?