?? or .GetValueOrDefault() [closed]
Asked Answered
J

2

7

I have properties type int? and decimal? that are being used in a calculation. If the value of any of them is null it has to default to 0. I'm trying to decide between using null-coalescing or GetValueOrDefault() which will also default to 0 if a value is null. Which approach would be better in terms of readability and performance (if there is any noticeable difference)?

First:

public decimal MyMethod(int memberId)
{ 
    var dto = GetDtoValue(memberId);
 return (dto.PropertyOne ?? 0)
      + (dto.PropertyTwo ?? 0)
      + (dto.PropertyThree ?? 0)
      - (dto.PropertyFour ?? 0)
      + ...
}

Second:

public decimal MyMethod(int memberId)
    { 
        var dto = GetDtoValue(memberId);
     return dto.PropertyOne.GetValueOrDefault())
          + dto.PropertyTwo.GetValueOrDefault())
          + dto.PropertyThree.GetValueOrDefault())
          - dto.PropertyFour.GetValueOrDefault())
          + ...
    }
Jadda answered 4/6, 2019 at 4:7 Comment(6)
Define what counts as “better” to you. I’d use ?? for clutter reasons, some may prefer the express readability of GVODHeptahedron
"better" would be code appearance/readability and performance (if there is any noticeable difference)Jadda
The first seems to be easier to read imho.Cattegat
I’m afraid this question is largely off topic for SO as it’s “primarily opinion based” being essentially “which of these looks better?”Heptahedron
@CaiusJard you are right, I personally think the top one looks better :-)Breakthrough
I don't like that this question was closed. For example you can objectively answer which option is faster (if there is a difference) or answer that there do the same stuff, there is no difference and they compile to the same code. I think questions like "I can do a thing in two ways, what is better" is fine, if you think it's opinion base then think of it as if person asked "I can do a thing in two ways, what is the difference between them". Because this is what the question is really about.Jeb
G
10

Readability

This is opinion of course, but

  • I read ?? 0 and feel like it's clear immediately
  • I read GetValueOrDefault and have to take a second to think about the type and then that type's default value (and I don't see this as a problem, just pointing out the "mental mapping")

Performance

This article holds that GetValueOrDefault compiles to CIL that's technically faster, but the improvement is a micro-optimization at best.

SharpLab, however, has both versions compiling to the exact same CIL (via Marc's comment below), which would make their performance identical.

Either way, the performance difference is insignificant at best, and nonexistent at worst, which means that readability should be prioritized.

Gondolier answered 4/6, 2019 at 4:31 Comment(1)
There is no performance difference - see: sharplab.io/…Tepid
H
3

?? (null coalesce) will call GetValueOrDefault internally in this situation.

The performance difference will be insignificant (unless you have a performance problem), so all that's left is working out which way you think is more readable.

Check it out here.

Hereford answered 4/6, 2019 at 4:23 Comment(3)
I’m interested to know more about how the compiler knows to rewrite this as GVOD? I’d always assumed it was rewritten as an inline-if null checkHeptahedron
@CaiusJard note this is less to do with Null Coalesce and more to do with Nullable Types as they are a special case in the language in all sorts of ways, they are also an implementation detail.Hereford
Perhaps a better example: sharplab.io/…Tepid

© 2022 - 2024 — McMap. All rights reserved.