Closed prdoyle closed 10 months ago
Would it be better to make the new submitReplacement(ref, Optional.of(val))
default and have it call submitReplacement(ref, val)
or submitDeletion(ref)
depending on if newValue.isPresent()
?
@gradycsjohnson - I just saw your question, sorry for the delay!
The attractive thing about using Optional
is that it would mean driver implementations need only implement two submit
methods instead of five. If we add the Optional
versions as default
methods on the interface and leave the existing five methods unimplemented, we don't get this benefit.
However, it's a fine first step if you want to start with that!
Right! Sure I'll go back to the first approach :)
To be clear... your way is much less effort, and is not a breaking change. If you wanted to start with that, I'm onboard.
If you're up for doing the whole thing, that's awesome too! It will affect every BoskDriver
implementation in the codebase (in a good way! That change will probably delete much more code than it adds).
I'm feeling like this might not be an improvement after all. The proliferation of Optional
arguments seems to have its downsides, and really, five methods instead of two isn't really that many.
Background
BoskDriver has five update methods:
submitReplacement
andsubmitDeletion
submitConditionalReplacement
,submitConditionalDeletion
, andsubmitInitialization
Early, we considered using
submitReplacement(ref, null)
to represent deletion, but becausenull
is a very common symptom of programming mistakes, we wanted to make deletion more explicit.Problem
Five methods to implement updates with slightly different semantics seems a bit much. It's a burden on implementers, who often end up with five copies of a bunch of code.
Solution
The notion is to use another
null
-like value that is notnull
. The challenge is: this is not generally possible, becausenull
is the only value in Java that can be passed for any (non-primitive) type.One idea would be that the value argument of
submitReplacement
would be anOptional
or similar.We would then write the updates like this:
submitReplacement(ref, val)
would becomesubmitReplacement(ref, Optional.of(val))
submitDeletion(ref)
would becomesubmitReplacement(ref, Optional.empty())
We could do the same for the
requiredValue
ofsubmitConditionalReplacement
, and by passingOptional.empty()
for therequiredValue
, we then would no longer needsubmitInitialization
.This would reduce the replacement methods to two:
submitReplacement
andsubmitConditionalReplacement
.Transition
We could even keep the current five methods as
default
methods in theBoskDriver
interface, so users can call them for convenience, so they don't need to useOptional
if they don't want to.