Apex code formatting

I’ve been spending (wasting?) time on Salesforce Stack Exchange. Often code is posted and nearly always the formatting used gets in the way of understanding what the code is doing. I suggest this can muddle the thinking of the person writing the code, and certainly impacts those who have to pick the code up later.

So before contemplating gnarly logical problems in code, the first job is to remove the formatting noise. Having a Java background, my “go to” reference is Code Conventions for the Java Programming Language. But there are a couple of language mechanisms specific to Apex that are worth particular attention…

The first is the SOQL for loop that you should make your default pattern when querying and iterating over SObjects. (Some reasons why: where there are large numbers of SObjects, only a chunk of SObjects occupy heap space rather than all the SObjects; the scope of the variable is limited to the loop block; clear and elegant syntax.) But there can be a lot going on in the SOQL so give the SOQL the clarity it deserves and keep the line length reasonable by using line breaks:

public class MyClass {
    public void myMethod(Set<Id> accountIds) {
        for (Account a : [
                select Id, Name, BillingStreet, BillingCity, BillingState
                from Account
                where Id in :accountIds
                order by Name
                ]) {
            // Do something with each Account

The second is the support for named parameters in SObjects. Instead of creating an object with no values in its fields and then assigning values field by field, create the complete object, dedicating a separate line to each parameter:

private class MyClassTest {
    static void myTestName() {
        Account a = new Account(
                Name = 'Acme',
                BillingStreet = '123 The Street',
                BillingCity = 'The City',
                BillingState = 'The State'
        // Do something

The above examples also use these formatting ideas:

  • Indent consistently and by 4 spaces (not tabs)
  • Indent continuations (where lines get too long and need to be wrapped) by 8 spaces more than their containing block to distinguish them from the start of a new block
  • Eliminate all blank lines that don’t have a purpose
  • Unfortunately Apex is case insensitive, but that is no reason to not use consistent capitalization to distinguish e.g. between types and variables

4 thoughts on “Apex code formatting

  1. Good ideas, I follow the same style for long queries and instantiating new objects. Also, I really like all caps for SOQL statements ‘FROM, WHERE, etc.’ Not a fan of spaces over tabs though.

  2. I also prefer tabs over spaces, but each to their own. When I fix code formatting on SFSE (which is quite often) it’s not because it doesn’t match my style, but because it’s inconsistent with itself. My main focus when fixing the formatting is to just make it consistent and fix the indentation which is often messed up when code with spaces and tabs is pasted in. If the OP opens their braces on the same line then I use that throughout, if they use a new line for a new brace I stick with that.

    Basically, as long as it’s legible I generally don’t worry about the actual choices made.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s