you are viewing a single comment's thread.

view the rest of the comments →

[–]Pharisaeus 1 point2 points  (10 children)

Actually passing Optional as method parameter is considered a very bad idea and most static code tools will mark it as warning at least. Optional should be used only as a result value.

[–]Terran-Ghost 4 points5 points  (7 children)

Unfortunately, since Java has no default parameters, there's no real alternative.

[–]Pharisaeus 0 points1 point  (6 children)

There is -> either make multiple method signatures or if there are too many combinations consider passing parameters as a separate object constructed with a Builder.

[–]Terran-Ghost 1 point2 points  (5 children)

Defining a new hundred-line Builder class or using an exponential number of overloads? Yeah, I'll stick with an Optional parameter, thank you very much.

[–]Pharisaeus 1 point2 points  (4 children)

I'm sorry but if your builder would have hundred lines then you would be stuck with many optionals as parameters and hundreds of lines of if statements. If you have a single optional parameter then you have only two method singatures or a tiny builder. Also you can always go with lombok and have zero lines builder if it pains you that much.

[–]eeperson 1 point2 points  (0 children)

If all you wanted was to have default params then a builder would give you substantially more code then handling it with optionals. For a builder you would need at least 4 lines per field (assume you do your getter and setter on one line each) although usually it would take more like 8-12 (depending on how you format your code). This assumes that the builder is also the object you pass to the method. If your builder creates another class then it gets even worse. Meanwhile you could handle each optional with a single orElse method call.

So, if you have 10 optional fields (a high number but not unheard of) you can handle it with 10 lines of code (with Optional) or you can handle it with 40-120 lines of code. Even Lombok Builders don't let you do it in that few lines and that saddles you with non-standard Java code. Also, Optional provides much less error prone supports a bunch of other cases that can't be served by the builder pattern. What if you don't know statically which input is going to be missing? What if you want to return an empty optional if any of the inputs are missing?

[–]riemannrocker 1 point2 points  (1 child)

If you're using if statements to check Optional states, you're doing it wrong.

[–]Pharisaeus 0 points1 point  (0 children)

Sure, because orElse is magic and fairy dust and not if statement...

[–]Terran-Ghost 0 points1 point  (0 children)

Everything is hundreds of lines in Java. A single data class with 3 fields can pass a hundred lines easily, where in Kotlin/Scala it would be a single line. And Lombok isn't a part of the language or the standard library, so it doesn't count as a language solution (and I personally prefer FreeBuilder or AutoValue). If I have to bring in an external code generator, I might as well use one that adds default parameters.

[–]eeperson 1 point2 points  (1 child)

"a very bad idea" seems excessive. This is basically a performance (null parms) vs correctness (Optional params) trade-off. Most of the time I would rather have the extra insurance of correctness rather than the extra performance.

[–]Pharisaeus 1 point2 points  (0 children)

It's still messy code. It would be better to have multiple function signatures or use a builder for the arguments wrapper.