Apex Best PracticesJosep Vall-llovera - CTO at Clapps
19/06/2015
@valnavjo Valnavjo in/jvn84 Valnavjo
Introduction
2
#1 Naming conventions
3
1. SF doesn’t provide packages to organize classes.2. Use naming conventions (English).
#2 Singleton pattern
4
1. Same instance per execution request.2. Useful to avoid repeating unnecessary queries.
#3 Util classes
5
1. Always use Singleton Pattern + Utils class.2. For example, Record Types.3. Do not query the same RT more than once.
6
#4 Trigger Template1. Do not have more than one Trigger for the same
SObject.2. Salesforce doesn’t ensure the order of execution.3. Avoid chaos.
7
#5 Bulkify1. All logic methods must be prepared to deal with
collections.
#6 Group collections
8
1. Use Maps & Lists to group collections when bulkifying code.
#7 Interfaces
9
1. Use interfaces when they are necessary.2. Focus on productivity.
#8 Unit Tests
10
1. Unit Tests must be UNIT.2. Avoid @seeAllData = true (only for pricebooks).3. Use public class with @IsTest for data initialization.4. Just three things: prepare data, do test and assertions.
#9 Use System.Limits1. Avoid governor limits.2. For example, when performing a search with SOSL.
11
#10 Keep things simple
12
Logic
TriggerHandler
Utils
UI
API Helper
Trigger
Controller
Service
Unit Tests
Useful links1. Apex Code Best Practices2. Context Variable3. Triggers4. My Trigger Template5. Use of the Limits Apex Methods
13
Code resources
https://github.com/Valnavjo/sdgbarcelona.git
14
Thanks!
Q&A15
Top Related