Code Layouts and Formatting Salesforce

In our last post, we talk about Naming convention in Salesforce. In this post, we will talk about Code Layouts and Formatting in Salesforce. A good developer should strive to use a consistent layout and format. It will make life easy for other developers and code reviewers. Let’s see the Salesforce Code formatting best practices.

Different teams and projects may have different standards and when contributing to a project, you should follow your team and company standards. I have listed a few guidelines for Salesforce Developer to format the code.

Code Layouts and Formatting Salesforce

Some of the features of Code Layouts and Formatting in Salesforce you need to keep in mind when you are coding:

1. Code Indentation

Use four spaces per indentation level. Usually, we use the [Tab] key to indent, but most editors can be configured to insert spaces instead of actual tab characters. Each code level should begin 1 tab stop further in than the above level.

if( age > 24){
    return true;
} else {
    return false;

2. Spaces

White space is commonly used to enhance readability. Here is one example with no spaces

Integer i;

Now check the below code with proper white spaces

Integer i;
for(i=0; i<10; i++){
    System.debug("Value" + i);

3. Line Length

Make wrap-up your code lines within 80 characters and it visible within one page. If it a big then try to break it with the wrapping rules.

4. Wrapping Lines

Goal: make it readable. If it is not readable, it is not formatted properly. When an expression will not fit on a single line. Keeping lines to a small width allows scripts to be read in one direction (top to bottom) without scrolling back and forth horizontally. Break it according to these general principles.

  • Break after a comma and before an operator
  • limits lines to 80 chars.
  • Align the new line with the beginning of the expression at the same level as the previous line.
  • If the above rules lead to confusing code or to code that’s squished up against the right margin, just indent 8 spaces instead.

Here are some examples of breaking method calls:

someMethod(longExpression1, longExpression2, longExpression3,
        longExpression4, longExpression5);

var = someMethod1(longExpression1,

Following are two examples of breaking an arithmetic expression. The first is preferred, since the break occurs outside the parenthesized expression, which is at a higher level.

Following are two examples of indenting method declarations. The first is the conventional case. The second would shift the second and third lines to the far right if it used conventional indentation, so instead it indents only 8 spaces.

Line wrapping for if statements should generally use the 8-space rule, since conventional (4 space indentation makes seeing the body difficult).

5. Placement

Put declarations only at the beginning of blocks. (A block is any code surrounded by curly braces “{” and “}”.)

void myMethod() {
     if (condition) {
     } else {

6. Blank Lines

Blank lines improve readability by setting off sections of code that are logically related

  • Two blank Lines: Add two blank lines between sections of a source file. Always add between Class and Interface/Wrapper Class definitions in Salesforce.
  • One Blank Line: User one blank line between methods, between class level variable and method. Use between logical sections inside a method to improve readability.

7. Remove Debug Statements

Remove debug statements from the code once done with testing

8. SOQL Alignment

Split each logical grouping into it’s own line.

FROM Account
WHERE Name like '%ApexHours%'

If you are using vsCode then try to use this VsCode extension for code formatting.

Salesforce Code Formatting Tools

1. Code Indentation in the Developer Console

The new Fix Code Formatting feature in the Developer Console uses the Prettier code formatter to format your Aura components.

Fix Indentation

To Indent, the code in the open file, Select the code, select Edit | Fix Indentation, or press SHIFT+TAB.

Fix Code Formatting (supports only Aura components)

To Fix Formatting, the code in the open file, Select the code, select Edit | Fix Code Formatting. Or, press Ctrl+Alt+F.

2. Prettier Code Formatter

This Prettier code formatter supports Aura and Lightning Web Components (LWC) as well as standard file formats such as JSON, Markdown, HTML, and JavaScript. Prettier can also support Apex if you install Prettier Apex plugin. Learn more about Prettier Code Formatter. Feel free to provide more best practices. I would love to add those to this blog post

Amit Chaudhary
Amit Chaudhary

Amit Chaudhary is Salesforce Application & System Architect and working on Salesforce Platform since 2010. He is Salesforce MVP since 2017 and have 17 Salesforce Certificates.

He is a active blogger and founder of Apex Hours.

Articles: 465


  1. Curious about the Class Level Comments including modification log & creation date – why if we store everything in version control these days?

  2. I’m interested to hear if you intentionally changed the example for the Method level Comment?

    I’m not sure if it would matter if you use a different value.
    – @param vs @Parameters
    – @return vs @Returns

  3. Salesforce impose a 6MB character limit on Apex code for your entire code base (excluding comments).
    But – unfortunately whitespace is part of that. So your recommended formatting advice should be taken with some caution.
    And if people don’t use tabs instead of spaces for indentation, this very significantly eats into that quota.
    Does the ‘prettier’ code formatter (or anything else) solve this issue?

Leave a Reply

Your email address will not be published. Required fields are marked *