1

I'm currently reading some (old) mapping code from one domain model to another, and i've stumbled accross something. EDIT: The difference between these domain models are their vesions. So alot of fields are named with the suffi V2 or V3 for instance. the MigrateToLatest methods, are simply mapping functions that maps between the types. So for instance if MigrateToLatest is called on a list of element, each element calls it's MigrateToLatest method. If these elements again contains complex elements, these again calls MigrateToLatest.

Okay so theres alot of code that looks like this going on.

 return this.SomeFieldV3!= null ? this.SomeFieldV2.MigrateToLatest() : null;

But theres also some coe that looks like this

return this.SomeField = this.SomeField ?? null,

So I understand that in the first line, in order to avoid a null pointer exception, it's first checked that the field is not null, and else null is returned. But i don't really understand the second line - or at least I dont think i do...

I looked the ?? operator up, at MSDN, but it seems superfluous in this context. If the value is not null, then the field is returned, and if the field is not, null is returned anyway? :-S

To me it looks like these two lines are doing the same thing, but since I know for a fact that the same programmer wrote the entire mapping class, I can't shake the feeling that they are different - why else would someone choose to do the same thing in two different ways?

So are these two lines of code doing the same thing or not? And if they are which are the preferred use, and if they are not can someone please elaborate on the difference

Andersnk
  • 857
  • 11
  • 25
  • 4
    `this.SomeField.MigrateToLatest()` is probably not entirely the same thing as `this.SomeField`. Also, the second example assigns `this.SomeField` prior to returning that value. You might want to provide precise real-world examples. – BoltClock Nov 28 '14 at 09:43
  • 1
    I agree with @BoltClock. This question is too ambiguous to answer. – Patrick Hofman Nov 28 '14 at 09:43
  • 1
    But you are correct that `??` is superfluous in the second example – Rhumborl Nov 28 '14 at 09:44
  • The statement: "return this.SomeField = this.SomeField ?? null" is reduntant. "return this.someField" is enough. – Emil Lundin Nov 28 '14 at 09:48
  • 1
    The 2 lines are not doing the same thing and are not equivalent at all. The first avoids a null reference exception, the second one is totally pointless. – Ben Robinson Nov 28 '14 at 09:48
  • Thanks for all your comments - i added some additional information. – Andersnk Nov 28 '14 at 09:51
  • Its not the ?? operator but how you are using it is superfluous. Why would you check something for null and assign null if true. Simply assign to null right? – Piyush Parashar Nov 28 '14 at 09:52
  • @PiyushParashar - It's not simply assigning to `null`. It's only doing so when `this.SomeField` is `null`. So it superfluous in this code, but maybe it is there to highlight to the coder that this field could be `null`. Kind of like "inline commenting". – Enigmativity Nov 28 '14 at 09:55
  • Yes that is what I meant in this code. I dont mean the operator is superfluous. Also, I am not sure how this kind of coding can help the coder. If you are assigning something to null, does the previous value really matter? May be a different style of coding. – Piyush Parashar Nov 28 '14 at 10:00
  • @PiyushParashar - I think you're missing the point. It's not assigning it to `null` unless it is already `null`. I think that's why the code is written that way - to explicitly indicated that is **could** be `null`. – Enigmativity Nov 28 '14 at 10:46

1 Answers1

2

Read this answer to know more about null-coalescing operator.

stackoverflow.com/questions/446835/what-do-two-question-marks-together-mean-in-c

I can only say that they are doing the same thing, but first one is better and faster.

Because in the second case the RHS evaluates to this:

this.SomeField ?? null

Is equivalent to

this.SomeField != null ? this.SomeField : null;

So the entire code

return this.SomeField = this.SomeField ?? null;

evaluates to

return this.SomeField = this.SomeField != null ? this.SomeField : null;

As you can see there is code repetition which can be minimized by only using this:

return this.SomeField ?? null;
Community
  • 1
  • 1
Vineeth Chitteti
  • 1,454
  • 2
  • 14
  • 30