July 21, 2021
•
5 min
 read

The history of the Apex compiler

The Apex language on the Salesforce Platform – its history and evolution. From interpreter to compiler – how was the Apex compiler created?

Kamil Golis
Craftware
Share this post

When I started my journey with Salesforce, I had little idea what kind of programming language Apex actually was. Experienced colleagues simply kept saying it was a language similar to Java. And indeed, if we look at it in terms of syntax, that turns out to be true. You can even find this statement in the official Salesforce documentation. There are, of course, more similarities – for example, the code is compiled to bytecode and run on the Force.com platform (on a Java virtual machine).

Apex – what does the name actually stand for?

The name Apex can be decoded as Advanced Programming Experience. Admittedly, there is no official confirmation anywhere that this is how it should be read, and the documentation does not mention it. However, Salesforce has already accustomed us to names built around the word Experience, e.g. Lightning Experience or the recent renaming of Communities to Digital Experiences.

In the beginning there was the interpreter

But back to the history of the language itself: it was first rolled out with the Winter ’07 release. At that point, however, it was intended solely for Salesforce’s internal use and was to be made available to larger customers only after a short beta testing phase. The author of Apex, Parker Harris (incidentally one of Salesforce’s founders), stated that developers “...have to get used to a new programming language”, “it’s not a difficult task if they are used to database language syntax and Java. If they are not database developers, though, some concepts may be new to them”.

And indeed, at the time Apex did not differ syntactically from one of the most popular programming languages – Java.

Harris also assumed that Apex would not be meant for building UIs, as its main purpose was to provide an additional layer of abstraction over database transactions, handle them and enable simple transformation of data into objects. In the same year, Harris presented customers with a technology for building user interfaces – VisualForce, which, combined with Apex, was a milestone in the platform’s development.

A bull’s-eye – time for a compiler

Enabling developers to write code and run it on the Salesforce Platform opened up new perspectives for them. From then on, it became possible to implement complex business processes and deliver tailor-made solutions. Combined with VisualForce technology, Salesforce turned from a simple CRM into a universal environment. The concept was therefore changed, and the “No Software” slogan, popular in the platform’s early days, was replaced with a new one – “Platform as a Service”. To associate it with the platform, it was decided to name it Force.com. This was announced at the Dreamforce conference in 2008.

But back to Apex. The first version of the language had no compiler, and all code was interpreted at execution time. This caused certain inconveniences related to performance and the capabilities of the language itself – for example, it was impossible to deserialize JSON. Significant heap RAM consumption was also a problem. Initially, the heap size on the Salesforce Platform was a mere 3 MB.
Despite huge commercial success, work on the language did not slow down. As early as 2012, after nearly 4 years of intensive work, Salesforce presented its first Apex compiler. Admittedly, it was not a compiler as we imagine one today, as all compilation was performed in the cloud (specifically, on the Force.com platform). Thanks to the new compiler, Apex code was compiled directly to bytecode, which was then run on the Force.com platform. In the same year, heap RAM was increased from 3 to 6 MB.

Octopus tentacles

The first compiler was far from perfect, though. In one podcast, Chris Peterson – currently Apex Product Director – said he joined Salesforce when the first compiler was being rolled out, which at the time resembled “octopus tentacles”. He used this comparison because Apex was very tightly integrated with all of Salesforce’s systems and services. This was potentially a huge problem he had to tackle.

The question is: why?

On the horizon was the vision of building a new (and much better) development environment based on the Visual Studio Code editor. It had an undeniable advantage – it made it easy to use the Language Server protocol created by Microsoft. Thanks to this, there was no need to define the language syntax and validation directly in the editor; instead, a local compiler (running on your own computer) could be used to check the syntax of the code being written in real time via the server. However, the compiler of the time was not fit for this task, as it was too tightly integrated with all Salesforce services (which prevented it from working outside the platform). Once again, the product team had to improve the compiler.

A new level of quality

A few years later, the new compiler was ready. In 2017, a VSCode extension appeared that made it possible to work with code using the Language Server protocol. Work on this solution had most likely been going on for quite some time – exact information is lacking, but it is estimated to have taken place in 2015–2017. Chris Peterson recalls that when the VSCode plugin was released, users also received a local Apex compiler. It was not a “fully-fledged” compiler, though, as the compilation output was not saved anywhere and it lacked full integration with Salesforce services.
Nevertheless, it was another step forward in the development of both the compiler and the language itself. Chris’s team (which few people know) has been releasing weekly patches for Apex (or, when the problem is more serious, once per release). The language itself does not evolve much syntactically, but it is constantly being improved.

Evergreen, or Salesforce Functions

We finally arrive at the present day and the next challenge facing Apex. Some time ago, Salesforce presented project Evergreen – now called Salesforce Functions. Put simply, it involves extracting a piece of code into a function and running it in the cloud (similarly to AWS Lambda). Ultimately, the code can be written in several popular programming languages – including Apex. The challenge, however, is still Apex’s tight integration with the platform’s internal services. That is why yet another compiler is being developed, which is to come with a standard library, while all integrations with services are to be handled via APIs. This is essential because one of the capabilities of Salesforce Functions is to run functions locally for testing. That is exactly why all integrations with services have to sit, so to speak, outside the compiler.

It is already known that Salesforce Functions are to become generally available (GA) in the next release, so work on the next version of the compiler is most likely nearing completion.

Summary

Since its creation, Apex has had a clearly defined task, which it still fulfils today. Syntactically, it has received several interesting improvements, such as better control over returning null (the Safe Navigation Operator, which I wrote about in the article “The null value in Salesforce – Safe Navigation Operator”), improvements to SOQL queries (the FIELDS clause) and inherited sharing for classes. However, what is evolving the most remains invisible to developers – the stability of the language and the speed of instruction processing.

Practical guide

Turn the insight into
a next step.

Keep exploring

More thinking for the next decision