Naming Conventions:
Data Types
- Capitalize the first letter (e.g.
String)
Classes and Triggers
Classes and Triggers
- Capitalize the first letter (e.g.
AccountTriggerorAccountSearchController) - Triggers should be post-fixed with the word
Trigger - Controllers should be post-fixed with the word
Controller - Other class names should be descriptive (e.g.
EmailServiceorTaskManager)
Methods and Variables
Methods and Variables
- Use camel casing (e.g.
getAccounts) - Use method names that are descriptive, in this format:
verbNoun(e.g.getAccountsorvalidateInput) - Use variable names that are descriptive
- For maps try to use
keyValuesMaporvaluesByKey(e.g.accountContactsMaporcontactsByAccountId) - For booleans try to use
verbAdjective(e.g.isDuplicateorhasException)
- For maps try to use
Unit Tests
Unit Tests
- When creating a test class for
MyClassorSomeTriggerappendTestto the end of the class name (e.g.MyClassTest) - Use a sentence to name your unit test methods, in this format:
methodUnderTest_to_doWhat_on_condition(e.g.getAccounts_to_returnNothing_on_blankFilterorsendEmail_to_throwException_on_nullRecipient)methodUnderTestis the method being tested. If testing a trigger this would be the event firing the trigger (e.g.insertAccount)to_doWhatis what the test is asserting (e.g.to_insertCasemeans the test is asserting that a Case should be created)
Formatting
- Add comments to classes, public methods, code blocks, and anything that may not be easy to understand by another developer
- Write method/class headers using the JavaDoc format like so:
/**
* Queries for and returns Accounts by Id
* @param accountIds List of Account Ids
*/
public static Account[] getAccountsById(Id[] accountIds) { ... }- Each line should not exceed 120 characters. Continue your code on a new indented line if it's longer than 120 characters
- Remove unused variables, debug statements, and other junk
- Add comments to classes, public methods, code blocks, and anything that may not be easy to understand by another developer
- Write method/class headers using the JavaDoc format like so:
/**
* Queries for and returns Accounts by Id
* @param accountIds List of Account Ids
*/
public static Account[] getAccountsById(Id[] accountIds) { ... }- Each line should not exceed 120 characters. Continue your code on a new indented line if it's longer than 120 characters
- Remove unused variables, debug statements, and other junk
Structure:
Structure:
- Break up large code blocks into smaller methods
- Trigger logic should be placed in a corresponding handler class (e.g.
AccountTriggercallsAccountTriggerHandler) - Anything with potential for reuse should be abstracted away in a separate method or class
- Avoid copy/pasting code at all costs
Unit Testing:
Unit Testing:
- Each class and trigger must have a corresponding test class
- Integrations and end-to-end processes must have their own test classes
- Each unit test must have at least one assertion
- Try to cover as many test cases as possible (do not just test the happy path)
- Do not use
seeAllData=true - Use
Test.startTest()to reset the governors for properly isolating the unit-under-test - Use
Test.stopTest()before assertions and in other circumstances as needed
