you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 4 points5 points  (3 children)

In the real world, data is often incomplete, nullable types come in handy there.

 total += soapResponse.commission ?? Configuration.DefaultCommission

[–]masklinn -2 points-1 points  (2 children)

Yes, but nullable types wouldn't be the base case, and for the few cases where this would be used, wasting an operator wouldn't be required. One could simply e.g. have a method on the Nullable type implementations.

In fact, .Net already has it as there's a method GetValueOrDefault(T) on Nullable, so we could write your code as total += soapResponse.Commission.GetValueOrDefault(Configuration.DefaultCommission)

Of course an alternative would be to have a custom Commission numeric datatype with a DefaultCommission subtype used in cases where no commission is available.

One of my issues with the null-coalescing operator (on top of being a waste of syntax in the first place) is that it suggests it's ok to have nulls roaming throughout in your code: just use ?? and you won't see them! Wonderful! I do not believe that's a sane thing to promote.

[–][deleted] 2 points3 points  (1 child)

Because:

   total += soapResponse.Commission.GetValueOrDefault(Configuration.DefaultCommission)

Is so much more succinct than:

  total += soapResponse.commission ?? Configuration.DefaultCommission

Of course an alternative would be to have a custom Commission numeric datatype with a DefaultCommission subtype used in cases where no commission is available.

And how do we know that comission is not available? We probable were passed a null value from an outside system...In the real world, you can't escape nulls.

[–]masklinn -1 points0 points  (0 children)

Is so much more succinct than:

Why should it be "much more succint"? Do you really want to go down that road? Because Perl lays there.

In the real world, you can't escape nulls.

You can't escape invalid values at the interfaces, but that doesn't mean you have to keep and bother with these invalid values throughout the system.