last day (15 days later) » 

9:28 PM
-1
A: How to write the following method elegantly?

RiverWhy not just make the optional parameters default to null, and only change the values that aren't null: Party cloneParty(int i_Id, PartyRole i_PartyRole = null, DeletedState i_Deleted = null){ Party clone = #clone party from i_Id here clone.PartyRole = i_PartyRole ?? clone.PartyRole; ...

 
This assumes that null is not a value new value for a given reference-typed property/field.
 
@ChristopherHarris true, but that could be easily fixed by having some other default parameter value to signify "not set".
 
That's not easy or elegant. That would require a wrapper a la Nullable, unless you just intend on invalidating another value, such as "NotAValue" for a string argument, which is just shifting the issue.
 
@ChristopherHarris Both the parameters are references, so I'm assuming they'd create some other sentinel value that they know would never be used. No this is not simple or elegant, but this assumes you are using null for something already, which seems unlikely, or at least easily remedied, to me. The sentinel value creation would be a last resort.
 
What happens if PartyRole is an enum? You then need to include a "not a value" value in your enum. Does including the opportunity to assign invalid values in the rest of your code constitute an elegant solution, in your opinion? Why is a clone method dictating the rest of your application's design?
What happens if null is a valid value for any of those references? This solution does not account for that. You would end up using the original value, which is not guaranteed to be the value you want (null).
 
9:28 PM
@ChristopherHarris I've added a solution for the enum, that was a good point. We already discussed the null value scenario above...
 
Can you provide an example of how you would handle valid null values?
 
As I said above, you could use another value to signify the unset condition
But in most cases, you'd be better off ensuring null is not a valid value
 
Right, but I'm wondering what that value would be? Referential constant? Where would that be located, code-wise... On the class as a static property/field?
IMO null is the million dollar mistake, but that's why I'm asking... you can't ensure that null is an invalid value for any given reference.
 
Probably a static constant on the class yes. As I said above, this edge case requires use of less elegant techniques, but remains simple.
 
I mean, you can... but other people won't... so you can't, really.
 
9:31 PM
Again, this returns to our discussion on your post about whether other people will even use this method.
Basically, for me it boils down to YAGNI
 
IMO 1) if OP is asking, YAGNI doesn't apply, otherwise the question itself is useless. 2) . the most elegant solution doesn't even involve a method (aside from the actual cloning part).
don't understand why OP would just var clone = GetClone"(5); clone.Role = PartyRole.Whatever; and be done with it.
less work and the same amount of code, either way.
 
Yea, I feel that solution is probably better too, I was just trying to stay within the constraints of the question.
I guess I'll add it in to this answer
 
but yes, I totally see your point. my solution is definitely over-engineered.
been working on a public facing automation API, so overengineering is my job, ATM. :)
thanks for discussing. :)
 
Yea, I feel like your solution is the definition of over-engineered, but I definitely see how in an API setting that would be beneficial or even required
@ChristopherHarris do you feel this chat encompasses all our discussion on both of our answers? If so I propose deleting all our comments except the link to this chat, to reduce the clutter.
 
10:03 PM
Your answer does not yet contain an example of handling valid null values, but otherwise all of the comments have been addressed. I've started deleting mine.
:thumbsup:
 

last day (15 days later) »