A Salesforce developer’s job is not an easy one. Every day, we have to struggle with the platform limits and deliver solutions that fit within its specifications. It’s no different with Apex, the programming language we primarily use.

As a strongly typed language and derived from mainstream object-oriented languages such as Java or C#, Apex inherited a certain feature – the null value. Thus, we can read in the documentation that: All Apex types are implicitly nullable and can hold a null value returned from the operator.

This is one of those features that can lead to serious consequences if not used correctly. Over the years, many developers have found out about it, facing the so-called billion-dollar mistake. This phrase was introduced in 2009 by Tony Hoare, who spoke about the implementation of Null References in the ALGOL language in 1965. As he recalls years later, he did it because “it was easy to implement.”

Why is null so scary?

As developers, we need to consciously return a null value or predict where it might occur and secure our code accordingly.

In Apex, we usually use the if clause:

wartość null_klauzula if

In the above example, we need to check that the getProfileUrl method does not return a null value before we further processing the returned URL (in this case, use the toExternalForm method).

If we tried to assign the profileUrl variable without checking what the getProfileUrl method returns, we could get a NullPointerException.

Alternatively, we could ensure that the code does not retrieve an element from an empty list or array:

wartość null_pusta lista

If the database query does not return any records, the list will be empty, and thus retrieving the first element from it (with an index of zero) causes the system to throw the List index out of bounds exception. It is, therefore, necessary to check the list size and possibly stop further code processing when it is empty.

 

Safe Navigation Operator comes to the rescue!

In the Winter ’21 release, Salesforce decided to make life a little easier for developers by introducing the Safe Navigation Operator. Adding a new operator to the language reduces the amount of code written, thus making it safer and more readable.

The syntax of the new operator is as follows:

wartość null_linijka kodu

 

How does it work?

If the expression on the left side of the above formula is null, then the expression on the right is not evaluated at all.

So the first example can be written using the new operator in one code line:

wartość null_składnia nowego operatora

If the left side of the expression, in this case – the value returned from the getProfileUrl method, is null, then the right side of the expression is not evaluated at all. Thus we are assured of not using the toExternalForm method on a null.

Using this operator, we can write the second example as follows:

wartość null_return

As before, if the database query does not return a record, and thus the left side of the expression is null, we will not evaluate the right side, that is, in this particular case, we do not extract the Name field.

Another interesting fact about the new operator is that it also works in combined expressions or so-called expression chaining.

Suppose we have a method to execute on a list element:

wartość null_wyrażenie łączone

Everything will work correctly if the tab array has an element with index x, where calling the method returns the appropriate object, and the field can be extracted from it. If any of the string elements is null, the system will throw an exception.

As we saw earlier, protecting the array is one possibility, but we can still expect a null as the method’s result. This is where combining operators come to the rescue:

wartość null_wyrażenie łączone

This design gives us a safe code call because at each successive evaluation step we check that what we have on the expression’s left side is not a null. If we wanted to write the same thing without the Safe Navigation Operator, we would have to check each sequence separately, for example:

wartość null_wywołanie kodu

It is important to note one more aspect of the above notation. The method is called twice if the first two conditions are met! Of course, you can write this method’s result and check it with the if clause. But it often happens that we see such monstrosity in the code as shown above.  We should avoid such notations.

 

Where do we not use the new operator?

Safe Navigation Operator can be used in methods, fields, casts, query results, or call strings. However, there are a few cases where using an operator does not work:

  • In types and static expressions with a period, such as Trigger.new.
  • In static variable and method calls, such as Class.method() or Class.field.
  • When assigning a value to an expression, such as foo?.bar = 42; or ++foo?.bar;
  • In assigning expressions inside a SOQL query, such as [SELECT Name FROM Account WHERE Name = :someName?.query]

 

To sum up

Modern object-oriented programming languages are now being designed as null-safety languages. This is a bit of a trend that tries to make it easier for developers to protect their code from accidental or intentional occurrences of null values. Salesforce, by introducing a new operator into Apex, has made the age-old battle against nulls a little easier.

Author

  • Kamil Golis
  • Salesforce Developer
  • At Craftware since 2017 as a Salesforce developer, however, programming amateurishly since his first computer – Commodore 64. Passionate about mobile technologies and astronomy. Privately, a fan of horror movies and zombie-themed cinema. In his free time, he creates games and tinkers.

Editorial study
Anna Sawicka
Text revision
Kinga Kisielińska
Text Translation
Did you like my article?

If so, I invite you to the group of the best-informed blog readers. Join our newsletter and you will not miss any news.