Are there any documents out there that talk about proper formatting in apex? I have not been able to find too much on this subject (maybe my google skills are weak).

Some examples:

 //comment with no space
 // comment with space

 if (something == something) { String foo = "keep it in one?"; }

      String foo="Or do it this way?";

I realize that this is probably based off of the user's preference, but I'm wondering if there is a standard to do things a certain way in apex? Would writing the syntax one way or the other affect the performance?

  • 3
    There's a notable question somewhere on here - I did a quick search and couldn't find it, but it pretty much says "follow Java standards." Jul 1, 2015 at 14:02
  • Unless someone beats me to it, I'll post the link in an hour or so. Note that it's an unofficial standard. The salesforce.com team prefers Java syntax straight up.
    – sfdcfox
    Jul 1, 2015 at 14:04
  • Yes, Java standards needs to be followed here as mentioned earlier. Also, I do not think formatting/Indentation will have any impact on performance. It is more for readability and maintenance perspective. Performance will depend on how you use/define the code, just as an example, see this link salesforce.stackexchange.com/questions/80654/…
    – Dave
    Jul 1, 2015 at 15:28
  • github.com/PolarisProject/salesforceStyleGuide is a style guide being written (and is found on another question/answer here). I myself am working on some open-source stuff, including a guide of my own, but focused more on optimization and legibility.
    – sfdcfox
    Jul 1, 2015 at 18:15
  • Nice link. Was reading the guide and it seems that could turn into something useful. I personally use "Atom" as my text editor and a plugin would be great for apex. If the plugin would replace 4 spaces with a \t would also be a useful feature. And then formatting your code for you. Could be a good side project of mine now that i think of it. Great responses everyone. Very helpful
    – Tyager
    Jul 1, 2015 at 18:44

2 Answers 2


Like others have said, you pretty much keep to Java standards. The only thing that is 'Apex Specific' I would say is line indentation. You want to always use tabs for indentation instead of spaces specifically in Apex/VF mainly because there is a character count for orgs. This may not seem like a big deal but you never know what may happen in the future if the org you are working with ends up getting an extremely large code base.

  • 2
    I missed that from my answer! - It is important to take character count into account (pardon the pun) especially when working on a large product! Jul 1, 2015 at 15:39
  • 3
    I reduced ~1,000,000 characters to about ~766,000 in a code base I inherited in about 30 seconds simply replacing four spaces with tab characters in the Force.com IDE (Search > File > Regexp search four spaces, replace with \t). It's simple, and highly effective.
    – sfdcfox
    Jul 1, 2015 at 18:22
  • 3
    Replacing spaces with tabs to save space. Am I back in the 1980s? Gotta love this platform. Sigh.
    – Keith C
    Jul 1, 2015 at 19:41

The examples you have provided will not affect performance in anyway. However writing good code is important, it's worth taking a look at the Apex Code Best Practices to ensure you are writing efficient apex.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .