Salesforce Coding Standards

This is an unofficial list of Salesforce Coding Standards sourced from various guidelines & coding standards from the internet. Salesforce recommends following Java standards for naming, that is, classes start with a capital letter, methods start with a lowercase verb, and variable names should be meaningful.

  1. Class

Class name should be noun/s starting with capital letter and follow PascalCase. Since Salesforce has different types of classes, suffixes can be used to differentiate each.

public class PageUtils { ... } // Utility Class
public class UnitSchedule { ... } // Schedule Class
public class DealBatch { ... } // Batch Class
public class AccountTest { ... } // Test Class
public class CaseWrapper { ... } // Wrapper Class
public class ErrorLogController { ... } // Controller
public class OpportunityExtension { ... } // Extension
public class LeadTriggerHandler { ... } // Trigger handler
trigger LeadTrigger  // Trigger
  1. Method Name

Method names should contain a verb and follow mixed case which begins with a lowercase letter, and the first letter of each subsequent word is capitalized. Adjectives nouns may be included in method names.

public String getWayPoint () { ... }
  1. Interfaces

Interfaces follow PascalCase similar to Classes and it should be nouns or noun phrases, or adjectives that describe behavior.  They are often prefixed with “I” to indicate as an interface.

public interface IBatchable { ... }
  1. Variables

Class and instance variables must be meaningful and mixed case (camelBack), Declare all member variables at the top of a class, with static variables at the very top.

String userName {get; set;}
Boolean isError {get; set;} // Avoid using negative booleans. eg -  Boolean isNotError
Integer ftpPort {get; set;} // Use lowercase for abbreviations
  1. Constants

In Salesforce, constants are defined with static final keyword. These constants must use all uppercase and separate words using underscore.

static final Integer PRIVATE_INT_CONST = 200;
  1. Comments

  • Class Comments

Class comments are placed on the top of the class to provide description and copyright statement. It uses block comment of multiple lines and annotations may be included to state additional information.

/**
* Class description goes here.
* ...
* ...
* ...
*
* @author  Xenon
* @version 1.0
* @since   2018-03-31
**/
  • Method Comment

Method comments give description about related method. Similar to class comments, it uses block commenting style with annotations.

/**
 * Method description goes here
 *
 *
 *
 * @param input parameter #1
 * @param input parameter #2
 * @return return value
 */
  • Single-line comments

Short comments can appear on a single line indented to the level of the code that follows. If a comment can’t be written in a single line, it should follow the block comment format. These comments also can be placed as a trailing comment.

// your comment here
 
/* your comment here */

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.