Salesforce Naming Convention
Introduction
Why naming convention is important?
- Self-Documentation – Meaningful names self-document your code. Clean code is like a story that you can read and follow. Appropriate variable naming helps avoid the need for comments.
- Consistency – Naming conventions should be consistent across the whole project. Developers know what to expect, which can aid in understanding and debugging.
- Readability and Understandability – Code is for humans, not machines. We understand the code we create, but we should also consider developers who will maintain our code later. A clear naming convention reduces confusion and facilitates logical understanding.
- Maintainability – Modifications are easier to make in code we understand. Even if the code is not working, but you can understand it, you can also fix it.
Issue
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
~ Martin Fowler
Good naming is necessary for the code to be readable and maintainable.
The problem is that a lot of developers do not follow proper naming conventions. Code is becoming messy and hard to understand.
❌❌❌
map<string, string> mapAcc = new map<string, string>();
string temp1 = '...';
public static void fun() {
// ...
}
I really like an example from "Clean Code" by Robert Cecil Martin that shows the importance of proper naming convention:
❌❌❌
public List<Integer> getThem() {
List<Integer> list1 = new List<Integer>();
for (Integer x : theList) {
if (x == 4) {
list1.add(x);
}
}
return list1;
}
✅✅✅
public List<Integer> getFlaggedCells() {
List<Integer> flaggedCells = new List<Integer>();
for (Integer cell : gameBoard) {
if (cell == FLAGGED) {
flaggedCells.add(cell);
}
}
return flaggedCells;
}
Best Practice
- Use Intention-Revealing Name – Ensure that each name answers critical questions: why does it exist, what is its purpose, and how is it utilized?
- Avoid Disinformation – Steer clear of names that may lead to confusion and misinterpretation.
- Make Meaningful Distinctions – Clarify the distinctions between terms, for instance, differentiate between
name
andnameString
, or betweenaccounts
andaccountList
. Simplify variable names by eliminating unnecessary words such asthe
,String
, orList
. - Use Pronounceable Names – As stated by the quote, "If you can’t pronounce it, you can’t discuss it without sounding like an idiot" [1]. Opt for names that are easy to pronounce, facilitating effective communication and code explanation.
- Use Searchable Names – Avoid the use of magic numbers that may confuse. Instead, employ
final
orconst
variables to explicitly convey your intentions, such as `const WORK_DAYS_PER_WEEK = 5; - Pick One Word per Concept – particularly crucial in Apex programming, adhering to the principle of using one word per concept. Maintain consistency, for example, with names like
AccountSelector
andContactSelector
for services and utility classes. - Add Meaningful Context – Provide contextual clarity, especially in classes. For instance, the
state
variable in theAddress
class should represent something distinct from thestate
variable in theShipping
class.
UPPER CASE
When to use?
- SOQL Keywords
final
andconst
❌❌❌
select id, name from account
✅✅✅
SELECT Id, Name FROM Account
snake_case
const
andfinal
variables – onlyconst
that are stored as global const values
✅✅✅
public static final String PARTNER_RECORD_TYPE = 'Partner';
import { LightningElement } from 'lwc'
const COLUMNS = [
// ...
];
export default class MyComponent extends LightningElement {
columns = COLUMNS;
}
PascalCase
- Apex Class Name
public with sharing class MyController {
//...
}
- LWC Component Name
import { LightningElement } from "lwc";
export default class MyComponent extends LightningElement {
//...
}
❌❌❌
string userName = '...';
boolean isPartner = false;
id accountId = '...';
✅✅✅
String userName = '...';
Boolean isPartner = false;
Id accountId = '...';
- Collection Type
❌❌❌
list<string> accounts = new list<string>();
set<string> names = new set<string>();
map<id, account> idToAccount = new map<id, account>();
✅✅✅
List<String> accounts = new List<String>();
Set<String> names = new Set<String>();
Map<Id, Account> idToAccount = new Map<Id, Account>();
- Object Name
❌❌❌
account myAccount = new account(...);
contact myContact = new contact(...);
SELECT Id, Name FROM account
✅✅✅
Account myAccount = new Account(...);
Contact myContact = new Contact(...);
SELECT Id, Name FROM Account
- Field Name
❌❌❌
SELECT id, name FROM Account
account acc = new account(
name = 'My Account',
industry = 'IT'
);
✅✅✅
SELECT Id, Name FROM Account
Account acc = new Account(
Name = 'My Account',
Industry = 'IT'
);
camelCase
- Methods
public String getUserId() {
// ...
}
handleClick(event) {
// ...
}
- Variables
let accountName = '...';
Boolean isPartner = false;
If you have any questions feel free to ask in the comment section below. 🙂
Was it helpful? Check out our other great posts here.
References
- [1] Clean Code – Robert C. Martin
- [2] Importance of Variable Naming
- [3] Naming conventions in programming – a review of scientific literature