Software Development

Freaky Apex Quirks - The Null That's Not Null

Cesar Parra
October 17, 2024

We all know that Apex is the most powerful tool we have to customize and extend Salesforce and, even though I find myself frustrated with the slow speed in which the language evolves compared to modern languages, I still find myself often being impressed by the fact that Salesforce allows us to do some of the things we can do with Apex.

But with all of its power, Apex does have its quirks. Writing and maintaining a programming language is hard, and the Apex team has the tough task of also wanting to maintain backwards compatibility for a very long time (maybe forever). This means that sometimes Apex is going to behave…weirdly.

While Apex is definitely not even remotely close to being as weird as Javascript, it does have its examples of weirdness. Here's one way I have found myself saying WTF Apex! (Wow, That’s Freaky Apex!).

The Null That’s Not Null

A few years ago the Safe Navigator Operator (?.) came to Apex. This now allows us to safely reference methods and properties of an object that might be null.

At the same time Apex, like any programming language in the world, also allows us to do comparisons between numbers, so we can do something like:

And if you do this, you’ll obviously get an exception:

So what happens when you combine the two concept? A WTF!

Now, I don’t want to blame the Safe Navigator Operator too much, let’s check out another iteration of the same concept, and getting some more freaky stuff through different means

***

NERD ALERT, do not read below this point, you have been warned ⚠️

I think I know what the issue is behind the scenes, but that requires me to talk a little bit about programming language theory. The issue is that I am declaring the variable as an Integer , if I declare the variable as anything else then Apex throws a compilation error, as expected.

When a piece of code gets interpreted by a language compiler, there are 2 passes that occur (there are more, but this is a gross oversimplification): compilation and runtime. Compilation is when the compiler tries to figure out what the text (the code) means, and runtime occurs when the code is actually running.

What seems to be happening is that the Apex compiler only has a compile time check to see if the comparison operations (==, >=, <=, etc.) are using matching types, but they didn't any any runtime checks. So, because I am declaring the variable as an Integer , the compilation pass does not throw any errors, and it lets the code run, and it seems that the runtime code simply returns false when it doesn't know what to do.

Cesar Parra
October 17, 2024

Related Articles