r/programming • u/bowbahdoe • Feb 08 '22
Java's Options for Options
https://mccue.dev/pages/2-8-22-options-for-options7
u/to_tgo Feb 08 '22
That is a pretty exhaustive list of options. Don't know if I saw it on there but I would first think composition / decorator to add functionality on there. Maybe also a bit of state pattern to manage the range of choices.
The moment you got over 3 choices and your code starts to bloat, it is time to find a strategy that'll simplify the process.
4
u/Strange_Meadowlark Feb 09 '22
Fantastic break-down!
I'm reminded of the Configuration Complexity Clock
Also, I think with the copyright message I'd kick that requirement back up and investigate handling that outside the JSON serializer. Setting a copyright message changes the format of the data and the other system has to be able to expect it. Maybe for uniformity's sake you need to just always have a wrapper object, e.g. { data: {...} } and { copyright: 'str', data: { ... } }. That way, the inclusion of the copyright property is modeled off of the model rather than some magic from the serializer.
That also simplifies the MessagePack issue -- just implement entirely separate serializers for each target format. Since the copyright message isn't handled in either format, there's no intersection between MessagePack and JSON.
Basically we're decomposing one object with many concerns into many objects with one concern (each).
1
u/on_the_dl Feb 09 '22
What about the JavaScript solution of passing in a lambda/functor?
JSON.stringify has this.
Did OP cover this option?
1
u/bowbahdoe Feb 09 '22
Can you give an example of that usage?
And from the looks of it, that would work for the copyright requirement. Take an arbitrary function that has preprocessing power over the value. Feel free to leave that one as a comment.
2
u/on_the_dl Feb 09 '22
I haven't used it in a while but I could imagine the visitor pattern. Basically, a JSON object is a recursive structure, right? So at the start of each call, you call a lambda that either returns a string it returns null. If it returns a string, emit that. If null, then just ignore the lambda.
You lambda might be, like, if it's a number then output the number rounded to the nearest .1. otherwise, return null.
The lambda probably needs to know the depth of recursion so that you can indent.
I haven't full thought it out but I know that JavaScript can do something like this. It's the second arg to JSON.stringify.
0
u/CurtainDog Feb 09 '22
This is essentially the https://en.wikipedia.org/wiki/Expression_problem Not exactly 'solved' but not particularly striking either.
I think you'll find Java's a bit like democracy - it's the worst programming language, except for all the others
0
Feb 08 '22
[deleted]
7
u/bowbahdoe Feb 08 '22 edited Feb 08 '22
That's option 19 but you don't call it options and expose the write method there.
It's also option 21.
No particular reason I didn't list stuff like composing objects a'la decorator pattern or separating steps of the toy problem so each individual one only needs one or two flags. Just kinda felt done near the end.
But sure, I'll "make a fucking class" and "learn what that means"
0
Feb 08 '22
[deleted]
6
u/bowbahdoe Feb 08 '22
That's 21, minus the interface
Unless you are talking about merging those two implementations, at which point you need to provide behavior for incompatible options
1
u/CodeLobe Feb 09 '22
Not reading the article... but doesn't Swing do something like this with constraints?
32
u/dnew Feb 08 '22
I'd suggest that maybe writing Java code where all the methods are static could be the problem.
This is what objects are for: to have behavior that is controlled by instance variables.
Stick required arguments in the constructor. Stick optional arguments in secondary calls. Finish up with your "final action" method. Adding new functionality just introduces a new method to say you want it, and everything is open for extension and closed for change.
Bonus points if your language supports typestate or invariants or anything else that would let you invalidate the object at compile time once it has been used up.