views:

53

answers:

2

Hello,

we identify client software using Version class and use these values extensively, we also store these values in database. How would you store unknown version value? We can get unknown version value if we wasn't able to successfully query client version.

I ask because I'm fond of Null object pattern, I don't like constant checks for null, and I wish that Version type was a value-type instead of reference-type. But Microsoft implemented Version type as reference-type. At the same time, in .NET v4 they added TryParse() method, which in case of failure saves result as (0,0) but not Null. If you call default constructor without providing exact version value, you would get (0,0) value, and internal fields get initialized to (0, 0, -1, -1). Perhaps, Microsoft realized, that Version should be value-type, who knows :).

But what is the best approach in handling unknown version values? Always check for null, or use default constructor?

+3  A: 

I'd be checking for Null. I think (0,0) is misleading in this context.

Andrew Cooper
Agreed, I think magic values should be avoided whenever possible
vc 74
This is not a Magic value, this is null object pattern. For example, we don't call string.Empty a magic value, and Guid.Empty also.
Dmitry Lobanov
+1  A: 

I would use the "default" value of (0,0) rather than null. As you stated, you won't have to deal with null checks (though, could easily be resolved using null coalescing), it's a value used by the framework as the default value (and failure condition), and I don't know of any situations where having version (0,0) makes sense anywhere. Also I was under the impression that assemblies must always have a version number. So a value of null might not always be available, particularly in an assembly. Or at least if this value is for your benefit, you should follow the pattern used by the framework.

Jeff M
My thoughts exactly. By the way, I wish that Version also define something like Version.Empty or Version.Default. Unfortunately there isn't any, and we can't add it since framework doesn't allow static extension methods and properties :(
Dmitry Lobanov
I agree, `Version.Empty` or similar would have been a nice thing to have. I guess we can expect to see that in the future in the value type, `VersionNumber`. :)
Jeff M