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
- Readability: Code Comment should be readable and clean
- Explain Logic or Reason: Your Code comment should not explain about what code is doing. It should give explanation why you are writing.
- Follow Standards: Follow Established Standards for JavaDoc or ApexDoc.
- Include JIRA Number: It always better to include Jira ticket in comment.
- 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.
token | Description |
@author | the author of the class |
@date | the date the class was first implemented |
@group | a group to display this class under, in the menu hierarchy |
@group-content | a relative path to a static html file that provides content about the group |
@description | one 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.
token | description |
@description | one or more lines that provide an overview of the method |
@param param name | a description of what the parameter does |
@return | a description of the return value from the method |
@example | Example 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.