We have over 270 uses where Nullable
Most of these use cases could be replaced with Nullable
(It is not LINQ!)
can I give this a go?
@georgettica Yes, feel free to pull PR. No need to fix all at one PR - please keep PR as small as possible. I suggest you make only changes of the same type in one PR - there are many different patterns and I suggest to fix only one pattern per PR.
wow! good job yall! I am gonna back off from the other PR's i've made and let you do your thing
thanks for all your support @iSazonov
and sry if I made you do double work
@iSazonov Please see my questions left in #13804 and #13793. It's not clear why changing in thsoe two places.
Quote my comment from https://github.com/PowerShell/PowerShell/pull/13804#discussion_r524868056:
I think the comment https://github.com/dotnet/runtime/issues/33792#issuecomment-649665286 in that issue has a point.
.Valueis semantically more correct given thatHasValueis already checked. There is a duplicate check on the booleanhasvaluefield, but I don't think that affects the perf in practice.For places where we can use
GetValueOrDefaultto replace both.HasValueand.Value, it makes more sense to make the change, like in #13805 and #13808. But if we need to keep the.HasValuecheck, then I think using.Valueafterwards is better.
I agree in common. Only there is another side - using different patterns decreases readability. I would prefer to use one pattern even though it looks unusual, especially since there are only a few of them in our code base. There is another reason why I prefer this. Now we often look at a code in .Net Runtime for a better understanding of how an application works and we consider this code as the best practice and high quality. Switching to a different code with a different style immediately causes discomfort. I believe that following .Net in this sense is the right direction - many of those who learn PowerShell code will be grateful to us.
If we're going to use GetValueOrDefault() there, we shouldn't _also_ be checking HasValue. It's fine to use one or the other, but using both is duplicating the same checks unnecessarily.
Only there is another side - using different patterns decreases readability.
This may be true to some syntax, but to Nullable<T>.Value and Nullable<T>.GetValueOrDefault(), I don't think readability would be affected given that the property/method names are so readable.
@vexx32 This could change execution logic - no value and default value can have different semantics in a code path.
This may be true to some syntax
Yes, I say about common rule we could/should follow but there are always exceptions to rule. :-)
When the .HasValue check is necesary, I think we should keep using .Value afterwards. It's semantically more clear that "I want the current value, not the default value". When the .HasValue is unnecessary, it makes sense to use .GetValueOrDefault(), like in #13805 and #13808.
@iSazonov If you agree, I think we should revert #13804 and #13793
I have no objection.
Most helpful comment
When the
.HasValuecheck is necesary, I think we should keep using.Valueafterwards. It's semantically more clear that "I want the current value, not the default value". When the.HasValueis unnecessary, it makes sense to use.GetValueOrDefault(), like in #13805 and #13808.@iSazonov If you agree, I think we should revert #13804 and #13793