In this article, we'll discuss the best practices for naming conventions in Salesforce and how to craft clean and readable code. Salesforce naming conventions serve as guidelines for choosing appropriate names for identifiers such as classes, variables, constants, methods, and more. While they are not mandatory, they are highly recommended for enhancing code readability and maintainability. These conventions are often considered principles rather than strict rules. By adhering to these naming conventions, you can significantly improve the clarity and manageability of your Salesforce applications.
Best Practices for Naming in Salesforce
We often find ourselves dedicating more time to reading our code than actually writing it. Hence, our code must be clean, concise, and easily comprehensible. Well-structured code enhances readability, promotes better understanding, and, in the end, contributes to the overall maintainability of the codebase. Before delving into Salesforce naming conventions, let's first explore the concepts of PascalCase, camelCase, and SNAKE_CASE.
camelCase: In each of these phrases, the words in the middle start with capital letters. For instance, "salesforceSherpa" follows this pattern.
String firstName;
PascalCase: It's similar to Camel Case, where the first letter is always capitalized, as exemplified by "SalesforecSherpa."
Class UserController{ }
kebab-case: The respective phrase is transformed into all lowercase with hyphens (-) used to separate the words, as seen in "salesforce-sherpa."
<c-hello-world-form></c-hello-world-form>
SNAKE_CASE:
Every word is represented in uppercase and separated by underscores (_), as illustrated in "SALEFORCE_SHERPA."private static final Integer MY_INT;
Utilize a postfix or suffix:
Maintaining consistent file naming practices is crucial for ensuring that components remain easily recognizable and searchable. Below are some examples of postfixes and suffixes that we have been employing in our project for an extended period.
Functional Type | Name Suffix | Examples |
Trigger | Trigger | UserTrigger |
Trigger Handler | TriggerHandler | UserTriggerHandler |
Trigge Action | TriggerAction | UserTriggerAction |
VF Controller | Controller | UserController |
VF Controller Extension | Ext | UserExt |
Service Class | Service | UserService |
Model / Wrapper Class | Wrapper | UserWrapper |
Web Service (SOAP) | Ws | UserToolsWs |
Web Service (REST) | Rest | UserCreateRest |
Email Service | EmlSvc | UserCreateEmlSvc |
Asynchronous (Future) | Async | UserCreateAsync |
Asynchronous (Batch) | Batch | UserCreateBatch |
Scheduled Apex | Job | UserCleanupJob |
Test Class | Test | UserCreateTest |
Queueable Apex | Que | UserSyncingQue |
Visualforce Page | -none- | UserClone |
Visualforce Component | Cmp | UserCloneCmp |
APEX NAMING GUIDELINES:
Class Name: Class names should be distinctive, commencing with an uppercase letter, and devoid of underscores or spaces (except for prefix and suffix considerations). These class names should consist of mixed-case nouns, where the initial letter of each word within the name is capitalized. For instance, as an example.
ClassNamePOSTFIX
Variable Name: Variables should adopt a mixed-case format, commencing with a lowercase initial letter, and employing capitalization for internal words. These variable names should be concise, meaningful, and follow the camelCase convention, such as "accountList" as an illustration.
List<Account> accountList;
Method Name: Methods should take the form of verbs, using mixed case with an initial lowercase letter and capitalizing the first letter of each internal word. It's important to utilize complete words and minimize the use of acronyms and abbreviations. The naming convention for methods should follow camelCase, such as "methodName," as an example.
showAccountDetail();
Constants: Variables declared as class constants should have names written entirely in uppercase, with words separated by underscores ("_"). This format should consist solely of uppercase letters, as exemplified by "CONSTANT_NAME," for instance.
private static final String ACCOUNT_LIMIT =’10’;
Trigger: <ObjectName>Trigger. This adheres to the Salesforce Trigger Patterns, which recommend having a single trigger per object.
UserTrigger
Visualforce Pages Naming Conventions
Visualforce page names and labels must be distinct, commencing with an uppercase letter, devoid of underscores or spaces. Words should be joined together using PascalCase, with the initial letter capitalized and subsequent internal words also capitalized. It's important to utilize complete words and minimize the use of acronyms or abbreviations, following a naming convention like the example provided.
AccountClone
Naming Conventions for Lightning Web Components
HTML File: When naming your component, employ camel case, and when referencing a component in the markup, use kebab-case. For instance:
helloWorld.html
JavaScript File: Your JavaScript class name should be in PascalCase, as illustrated in the following example:
export default class HelloWorld extends LightningElement{
}
CSS File: When naming methods in your CSS file, use verbs in mixed case, beginning with a lowercase letter and capitalizing the first letter of each internal word. It's important to use complete words and minimize the use of acronyms or abbreviations. The naming convention should be camelCase.
showAccountDetail();
Standards for Naming CSS Classes
CSS classes should be designated according to the specific component they are targeting. In cases where a name comprises more than one word, it should adopt the following format: "class-name," with multi-word names being separated by a hyphen ("-").
Lightning Component Naming Conventions
Lightning component names must possess uniqueness and start with a lowercase letter. All components should conclude with the "Cmp" suffix, as in "userCardCmp," following the pattern of an initial lowercase letter and the "Cmp" suffix.
userCardCmp (Initial lower case letter and suffixed with “Cmp” )
Lightning Events
Lightning event names should exhibit uniqueness and commence with a lowercase letter. All events should terminate with the "Evt" suffix, as exemplified by "userEvt." This convention involves starting with a lowercase letter and appending the "Evt" suffix.
userEvt (Initial lowercase letter and suffixed with “Evt” )