r/programming • • Feb 08 '22

Java's Options for Options

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

13 comments sorted by

32

u/dnew Feb 08 '22

I'd suggest that maybe writing Java code where all the methods are static could be the problem.

jw = new JsonWriter(myAppendable, myJson);
jw.indent(true).indentArrays(false).indentObjects(true)....write();

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.

6

u/masklinn Feb 09 '22 edited Feb 09 '22

The entire interface seems back-asswards to me.

Surely the appendable and json-able are passed to the final write method? There’s no reason to make them part of the writer, they’re stuff the writer acts upon, and that way you can configure the writer once and reuse it.

There’s no reason to invalidate anything either, the writer is completely stateless, it’s not a wrapper type.

1

u/dnew Feb 09 '22

That's another way to do it. I was speaking more generally, such that required arguments are required to create the object. Otherwise you have objects floating around that are in an invalid state. You could also have it such that there's a method to change the appendable and/or json inputs, while also passing them in the constructor since they're required. Otherwise, your object isn't a "print a json" object but a "configure how to print a json object".

Imagine, say, a send-pdf-to-printer object. You pass in the printer and the pdf to start, and then the options you can set will change on both. E.g., you can't set color=true unless both the printer supports color and the PDF includes color.

And if the thing is stateless, then sure, you can make an object that carries the configuration and has methods to execute whatever commands you want to send to it. I was just going with TFA's example.

2

u/FlatPlate Feb 09 '22

I think the design generally is kinda whacky. Why is it a JsonWriter and not JsonSerializer? Why is the method static? Why does the JsonWriter append the copyright to the output and not some other Middleware? I have so many questions.

7

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

u/[deleted] 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

u/[deleted] 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?