17 October 2020

Fast Feedback for Apex Developers

Context

I’ve been doing a lot of Apex development with other developers lately and I’ve enjoyed showing them this technique for going faster. I thought I’d share it in case other people didn’t know this one…​

Problem

Apex has a remote compiler and execution engine only. This introduces a lot of latency when writing/testing Apex code.

Writing Java or C# has two tools that don’t suffer from this latency:

  1. The IDE can statically check syntax and other structural rules

  2. A local watcher can re-run tests with mocks for all remote/slow services and provide fast feedback

Writing Clojure takes it to another level by allowing interactive evaluation of code using the REPL.

How can we have this when writing Apex? We already have static code checkers in our IDEs but what about fast compile/run?

Solution

TL;DR use Execute Anonymous more!

When using the Execute Anonymous capability of your favourite IDE, you can get much faster feedback. The delay is normally 1-2 seconds. This is better than 12 seconds to compile and 10-20 seconds to run a test.

If you can automatically filter System.debug in the log/results, it makes you faster again. I use Illuminated Cloud for this (and many other) features.

You might think this is not really practical because you need your code in methods. Did you know you can declare methods in an execute anonymous block?

apex anon methods

Combined with a keyboard shortcut to run this code (CMD-Enter for OSX/Illuminated Cloud), this is a big speed gain. Once you have the methods working, you can paste them into a class and then work at the next level up.

Maybe you already knew this? That’s ok but I bet there’s a few people out there who didn’t so I hope someone finds it useful.

If this is well received, I’ll start sharing more of these tricks. I’ll watch Twitter and LinkedIn likes/re-tweets for that signal.

Tags: Salesforce