Coding Tips And Tricks: Null Checks

Coding Tips And Tricks: Null Checks

Null Conditional

There is a feature in c# where you can add a null check by putting a question mark before the dot when reading a property, field, or function from an object and if the object is null, you will get the a null result, and if the object isn’t nullable like a boolean or an integer then what you get back is a nullable<bool> or nullabe<int>. Which can be implicitly converted back into a bool or int, with null converting to the default value.

ex:

List<LaminaSaveData> ownedLamina = GameData.OwnedLamina;   
if (ownedLamina?.Count <= 0)
    { return; }

When ownedLamina is null, ownedLamina?.Count will return the default value for count which being an integer is 0. So this if statement essentially says “if owned lamina is null OR has a count of 0”

Here’s a common gotcha when working with the null conditional operator. The assumed reaction was that the default value being 0 would work, but when using the null conditional operator, it doesn’t return the default value that you are assigning, it returns it based on what you are calling.

So, in this example, since data.Test return float. data?.Test() returns float? or when written longhand Nullable<float>. Here is an example that you can use however.

  public void NullTest()
  {
    Experiment data=null;
    int xxx = (int)data?.Test();
    int yyy = (int)data.Test();
  }

Notice that whether you are working with a float, or a nullable float, you have to convert it before assigning it to an integer.

Another gotcha when working with unity specifically is Monobehaviors. Once you get used to using the null conditional operator, you’ll start using it without even thinking, but that can get dangerous because Unity has overridden the standard c# equality checks. When you call “monobehavior == null” you are not really checking if it is ‘null’ in the c# sense, but rather you are checking to see if the monobehavior still exists in the processing thread basically. Is it active & getting messages like update in the normal queue.

People have very different opinions on whether this is a good or bad thing. Personally, I feel like it’s convenient and I haven’t run into any scenario where this has actually tripped me up, apart from using either the Null Conditional or the Null Coalescing, and that’s only because they haven’t provided their own versions of these two implementations.

The rule of thumb when working with anything that derives from unity’s Object class is to avoid these two features until they either extend it to meet these two features, or remove it entirely which would come with it’s own basket of problems.

You can read unity’s current stance on it here.


Null Coalescing

Null coalescing is very similar to null conditional. It uses two question marks and basically acts like the && or || operators.

A ?? B => If A is not null, return A. Else return B

A || B => If A is true, return A. Else return B

A && B => if A is false return A. Else return B

This can even be used in conjunction with the Null Conditional. For example

string observerName = value?.gameObject.name ?? string.Empty;

If the value is null, return string.Empty, otherwise return the game object’s name.