Code Layouts and Formatting Salesforce : Amit Chaudhary

Code Layouts and Formatting Salesforce
by: Amit Chaudhary
blow post content copied from  Apex Hours
click here to view original post


In our last post we talk about Naming convention in Salesforce. In this post we will talk about code layout and formatting. A good developer should strive to use a consistent layout and format. It will make the life easy for other developers and code reviewer.

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 Comments Best Practices

When any developer look into your code he should understand what is going in your code easily. Means your code should be “self-documented”.

  • Class Level Comment : All Classes and Triggers should begin with a brief comment describing the functional
/*
*********************************************************
Apex Class Name : MyController
Created Date    : July 12, 2020
Description        : This is class is used for....
Created by         : Amit Chaudhary
Modification Log:
*********************************************************
*/
  • Method level Comment : All methods must have a @Description section, describing what the method is designed to process. They should have @param section for input  parameters and @return for output.
/*
*********************************************************
@Method Name    : createUser
@Description    : method to is used to create usser
@Parameters    :
@Returns        : Output
********************************************************
*/

Layouts and Formatting

Some of features you need to keep in mind when you are coding

Code Indentation

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

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

Spaces

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

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

Now check below code with proper white spaces

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

Line Length

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

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
  • limit lines to 80 chars.
  • Align the new line with the beginning of the expression at the same level on 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,
                someMethod2(longExpression2,
                        longExpression3));

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).

Placement

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

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

Blank Lines

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

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

Remove Debug Statements

Remove debug statements from the code once done with testing

SOQL Alignment

Split each logical grouping into it’s own line.

SELECT Id,
       Name
FROM Account
WHERE Name like '%ApexHours%'
LIMIT 10

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

Code Indentation in Developer console

The new Fix Code Formatting feature in 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.

Feel free to provide more best practices. I would love to add those in this blog post

The post Code Layouts and Formatting Salesforce appeared first on Apex Hours.


March 22, 2021 at 04:27AM
Click here for more details...

=============================
The original post is available in Apex Hours by Amit Chaudhary
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Salesforce