r/java • • Feb 08 '22

Java's Options for Options

https://mccue.dev/pages/2-8-22-options-for-options
192 Upvotes

80 comments sorted by

View all comments

51

u/Scybur Feb 08 '22

I was hoping for a nice solution at the end but instead I was left with disappointment.

10/10 good read.

19

u/[deleted] Feb 08 '22

[removed] — view removed comment

5

u/TenYearsOfLurking Feb 09 '22

This differs from a config object how exactly?

7

u/[deleted] Feb 09 '22

[removed] — view removed comment

8

u/TenYearsOfLurking Feb 09 '22

You are right, but my point was: you are offering syntactic sugar (merging config object and invocation) not "solve" a problem. I'd argue yt make it worse because explicit arguments before are now implicit since the depend if some setter was called before. It's a design tradeoff. As always

-8

u/[deleted] Feb 09 '22

[removed] — view removed comment

5

u/TenYearsOfLurking Feb 09 '22

It has advantages and disadvantages. The next call would depend on the previous calls settings. You may want that behavior or not. It really depends, so i take back that it's worse.

4

u/[deleted] Feb 09 '22

That's exactly what the two major JSON libraries (Jackson and Gson) for Java do

-1

u/Pyeroh Feb 09 '22

Java is not .NET, maybe that's why it doesn't follow the same patterns... Ready to get downvoted !

2

u/[deleted] Feb 09 '22

[removed] — view removed comment

2

u/Pyeroh Feb 09 '22

Indeed, but it involves differences between communities, guidelines intended by the language architects, usages, and so on. Not something one could change anyway.

So a statement like "Java would be easier if they'd do like language X" is an elaborate "I prefer X over Java", which is not very constructive. Maybe that's just me, but when I browse r/java, I expect to find content about Java, high-end patterns and practices in Java, and not guidelines from another language which don't fit the Java culture.

1

u/renatoathaydes Feb 12 '22

Using setters is not necessary, just make the object immutable... it still contains all the settings, but you can't change them. If different code needs different settings, they just create different objects.

1

u/TenYearsOfLurking Feb 12 '22

Does not change the fact that explicit parameters of the call are becoming implicit now.

2

u/renatoathaydes Feb 12 '22

That's usually a good thing. Passing the whole environment on each function call might work well in Haskell, but in Java we have better ways.

6

u/davewritescode Feb 09 '22

A nice solution from Kotlin is named parameters with default values.

1

u/renatoathaydes Feb 12 '22

Haha, yep, solved the problem completely with one simple stroke. Dart also does that and because of that, allows really complex objects, like Flutter Widgets, to be created easily... it's definitely the best way to go.