Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 6.0 multiple identical null conditional operator checks vs single traditional check

Which out of the following two equivalent ways would be best for the null conditional operator in terms of primarily performance and then ease of use or clarity etc.?

This:

idString = child?.Id;
fatherName = child?.Father?.Name;
motherName = child?.Mother?.Name;

or (assuming that all local vars are null already) this:

if (child != null)
{
    idString = child.Id;
    fatherName = child.Father?.Name;
    motherName = child.Mother?.Name;    
}

Is performance even an issue?

like image 563
GDS Avatar asked Dec 06 '16 09:12

GDS


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

Is performance even an issue?

Short answer: Performance about null-checks will never be an issue in a normal application. It's only a matter of readability and maintainability.

Performance:

Yes, you have 3 "explicit" checks against 1. But you must keep in mind that:

  1. The system performs an "implicit" null-check every time you reference an object instance, as explained here, and afaik the JIT doesn't optimize null-checks at all. So in your case the rate is not just 3 against 1.
  2. A null-check is a very (really, very) cheap operation, compared with most operations you do in a normal software flow (instance heap allocation, mathematic calculations, a linq query, graphic object rendering, string parsing, ...).

I see only a remote possibility of significant performance differences: if child or Mother or Father are not local variables or simple plain properties, but methods and properties with very long execution time. For example, a GetChild() method that takes 2 sec to execute. Can you see a realistic scenario for that? I can't. Even if that's the case, you can call GetChild() one single time and assign it to a local variable, then call child? 3 times.

Readability:

A single initial if allows to mentally separate different chunks of code. Pretend to be the reader of the code, without knowing anything else: ask yourself if is it simpler to read "if child is not null do all this operations and stuffs, otherwise just move on", or "if child is not null check the Name. If again child is not null check the Mother. If the Mother is not null get the Mother's Name. Then if again child is not null check the Father. If Father is not null... ... ... ".

Maintainability:

Aka, in this case, the DRY principle. For example, why would you repeat the null-check 3 times? Pretend that at a certain point in the future your boss asks to you a code change: it is required not only to check the nullity of a child, but also if its Id is 0 (things like that happen very frequently in any software development process). In your first code section you should correct 3 lines. In your second code section you should correct only 1 line: the initial if.

EDIT:

For a discussion about thread-safety on the null conditional operator, see this question.

like image 186
Massimiliano Kraus Avatar answered Oct 09 '22 11:10

Massimiliano Kraus


In second code example for variables will not set new values, but in first example they set as null or value from specified properties.

The ?. operator names like null-conditional operator. This operator works like:

With using ?.:

var result = someProperty?.someField;

Without using ?.:

if (someProperty != null)
    result = someProperty.someField;
else
    result = null;

About this operator you can read here: https://msdn.microsoft.com/en-us/library/dn986595.aspx.

It is better to use it in fluent of methods invoking. In your sample better to use second variant, because if child is null, then other actions are not perform.

like image 1
EgoPingvina Avatar answered Oct 09 '22 10:10

EgoPingvina