Salesforce Code Comments best practices

Comments are an important part of writing code in Salesforce. At the same time, this is one of the most abused things as well for writing no comment and writing too much comment. Let see the Salesforce Code Comments best practices.

Code Comments best practices

  1. Readability: Code Comment should be readable and clean
  2. Explain Logic or Reason: Your Code comment should not explain about what code is doing. It should give explanation why you are writing.
  3. Follow Standards: Follow Established Standards for JavaDoc or ApexDoc.
  4. Include JIRA Number: It always better to include Jira ticket in comment.
  5. Commit: Don’t commit the Code without comment

Code comment in Salesforce.

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

1. 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....
@author            : Amit Chaudhary
Modification Log:
Ver   Date         Author                               Modification
1.0   04-01-2021   Amit Chaudhary                      Initial Version
*********************************************************
*/

Located in the lines above the class declaration. The special tokens are all optional.

Located in the lines above the class declaration. The special tokens are all optional.

tokenDescription
@authorthe author of the class
@datethe date the class was first implemented
@groupa group to display this class under, in the menu hierarchy
@group-contenta relative path to a static html file that provides content about the group
@descriptionone or more lines that provide an overview of the class

2. 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
@author         : Amit Chaudhary
@description    : method to is used to create usser
@param          :
@return         : Output
********************************************************
*/

In order for ApexDoc to identify class methods, the method line must contain an explicit scope (global, public, private, testMethod, webService). The comment block is located in the lines above a method.

In order for ApexDoc to identify class methods, the method line must contain an explicit scope (global, public, private, testMethod, webService). The comment block is located in the lines above a method.

tokendescription
@descriptionone or more lines that provide an overview of the method
@param param namea description of what the parameter does
@returna description of the return value from the method
@exampleExample code usage. This will be wrapped in tags to preserve whitespace

Further learning

Summary

I hope this Salesforce Code Comments best practices will help you to write clean code in Salesforce. Let us know how you maintain clean code in your org.

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: 460

Leave a Reply

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