Let’s say I have double length that can be either a real length or not ready yet since we got no length yet in the server and there is nothing to send to the client. We need to pass this length from the server to the client as part of a fixed data protocol. The client currently uses the length only once, but might use it more than that in the future.
-
Pass double length and bool isLengthValid, and in every place you use length, check if isLengthValid
-Clean design without mixing data types but user have to remember to check -
Pass double? length, and in every place you use length, check if length==null
-Design is clear (since it’s a nullable) but if you look and the type. Also – there will be an exception if someone uses without checking (good and bad, depends how you look at it) -
Make a class Length instead of double. The class will have a clear interface of GetLengthIfYouCheckedIt or something.
Very readable and hard to make mistakes but design is a little over done.
What is your solution?
I say option2:
Nullable<double>.HasValue, just as it was meant for it.Double. Think of how many of such classes you’ll have to make and maintain for TIME/DateTime, MONEY/Decimaletc. It will never end.Nullable<T>rewrapped with another name.In other words, enforce the DRY principle, and use
Nullable<T>😉HTH,
Bab.