Piotr Gajek
written byPiotr Gajek
posted on January 5, 2024
Technical Architect and Full-stack Salesforce Developer. He started his adventure with Salesforce in 2017. Clean code lover and thoughtful solutions enthusiast.

Salesforce Naming Convention

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 and nameString, or between accounts and accountList. Simplify variable names by eliminating unnecessary words such as the, String, or List.
  • 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 or const 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 and ContactSelector for services and utility classes.
  • Add Meaningful Context - Provide contextual clarity, especially in classes. For instance, the state variable in the Address class should represent something distinct from the state variable in the Shipping class.

UPPER CASE

When to use?

❌❌❌

select id, name from account

✅✅✅

SELECT Id, Name FROM Account

snake_case

  • const and final variables - only const 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

Buy Me A Coffee