In any environment, non-existent or ad-hoc standards greatly contribute to technical debt. Join us as we explain why Salesforce's multi-tenant architecture and its platform and governor limits make managing technical debt in the App Cloud so critical. You'll discover five best-practices that can make an immediate impact on the maintainability and scalability of your org.
3. Best Practices We’ll Cover
• Adopt a standard Coding Style
• Follow trigger best practices
• Follow unit test best practices
• Adopt a version of “Separation of Concerns”
• Adopt specific naming conventions
How Each Will Be Presented
• Key benefits
• Adoption challenges
• How to execute
• Demo
Overview
4. • Clean, consistent code
• Easier to understand / maintain
• Demonstrates who is willing to
change
• Difficult to know where to start
• Difficult to agree on standards
• Can be like discussing religion!
• Difficult to enforce
• Involve the whole team
• Don’t start from scratch
• Borrow from the best
• Write it down
• Code review
Key Benefits Adoption Challenges How to Execute
Best Practice #1
Adopt a Coding Style
5. • Scalable code
• Maintainable code
• Extensible code
• Ensuring every developer knows
and understands best practices
• Choosing a “standard” pattern to
follow can be challenging
• Refactoring code can be painful
(even unwanted)
• Understand the best practices
• Choose a pattern
• Create reference code (ie.
templates / boilerplate)
• Leverage code library / snippet
tools
• Code review
Key Benefits Adoption Challenges How to Execute
Best Practice #2
Follow Trigger Best Practices
6. • Significant improvement to code
quality
• Better prevention of regressions
• Faster deployments
• Writing good unit tests is hard
• Payoff is invisible
• Nobody likes writing test classes
• Test classes are often written at
the last minute
• Really difficult to enforce
• Learn about the best practices
• Attitude adjustment!
• Consider test-driven
development
• Leverage tools like the Force.com
Security Scanner
• Code Review
Key Benefits Adoption Challenges How to Execute
Best Practice #3
Follow Unit Test Best Practices
7. • Reusable, extensible code
• Easier maintenance
• Greater agility
• Hard to implement
• Confusing (until it’s not!)
• Developer turnover can hinder
adoption
• Grab the low-hanging fruit
• Target an “express” version of
SOC
• Document it, and get developer
acknowledgement
• Naming conventions are critical
• You guessed it...Code Review!
Key Benefits Adoption Challenges How to Execute
Best Practice #4
Adopt a Version of “Separation of Concerns”
8. Separation of Concerns (High Level)
Entrypoint Layer
Invocation Layer
Service Layer
Utility Layer
Activities that invoke Apex
• API / UI Based Create/Update/Delete
• Visualforce Page / Component Load
• SFDC Asynchronous Queue, Apex
Scheduler
• Visualforce Controllers and Extensions
• Triggers and Trigger Handlers
• Web and Email Services
• Asynchronous and Scheduled Apex
• LeadServices.cls, AccountServices.cls, etc.
• RegistrationServices.cls, CpqServices.cls, etc.
• Utility Classes
• Model / Wrapper Classes
• Selector Classes
Domain and Function-
specific
Business Logic
“First Line” of Apex Code
Supports the Service Layer
9. Separation of Concerns (Low Level)
LeadServices.cls AccountServices.clsContactServices.cls
LeadConversionUtil.cls ConvertedLead.clsContactSelector.cls AccountSelector.cls
LeadConv.page Apex Scheduler
SFDC
Asynchronous
Queue
API Based
Create/Edit/Delete
Operation
UI Based
Create/Edit/Delete
Operation
LeadConvController.cls
LeadTrigger.trigger
LeadTriggerHandler.cls
LeadIntAsync.cls LeadCleanJob.clsLeadIntBatch.cls
Entrypoint Layer
Service Layer
Invocation Layer
Utility Layer
10. • Simplifies implementation of
Separation of Concerns
• Code is easier to understand and
maintain
• Putting thought into names can
encourage better design
• Choosing a standard is hard
• Refactoring metadata names in
production is painful
• Difficult to enforce
• Choose standards that make
sense to your organization
• Document your standards
• Code review…before deploying to
production!
Key Benefits Adoption Challenges How to Execute
Best Practice #5
Adopt Specific Naming Conventions
11. Apex Class and Trigger Names
Functional Type Related Component Related Function Name Suffix Examples
Trigger Account.object Trigger AccountTrigger
Trigger Handler Account.object TriggerHandler AccountTriggerHandler
VF Controller NewAccount.page Controller NewAccountController
Service Class Account.object Services AccountServices
Utility Class Account.object
Identifies duplicate
Accounts
Util AccountDupeCatcherUtil
Web Service (REST) Account.object
Exposes specialized
Account data tools
Rest AccountToolsRest
Asynchronous (Future) Account.object
Performs asynchronous
integration tasks
Async AccountIntegrationsAsync
Asynchronous (Batch) Account.object
Performs high-volume
data migration tasks
Batch AccountMigrationBatch
Scheduled Apex Account.object
Performs data cleanup on
a regular basis
Job AccountCleanupJob
12. Make sure that you...
• Stay consistent
• Keep a list of commonly used abbreviations
• Catch bad names before they go to production
Key Considerations When Naming Things
13. • Starts at the top, but has to be a team effort to be
effective in the long term
• Clear, concise, easy to access documentation is
key
• When the going gets rough, keep on going
• Persistence is better than perfection!
Putting it All Together
The Importance of Governance
14. Adopt a Coding Style
• Google’s Java Style Guide
Trigger Best Practices
• Force.com Apex Developer’s Guide - Triggers
• Trigger Frameworks and Apex Trigger Best
Practices
• The Simple Trigger Pattern (STP)
• GitHub Gists
• GistBox
Unit Test Best Practices
• An Introduction to Apex Code Test Methods
• How to Write Good Unit Tests
• Apex Annotations
• Force.com Security Source Scanner
Separation of Concerns
• Separation of Concerns – Wikipedia
• Apex Enterprise Patterns – Separation of Concerns
• DF’12 Recording – Applying Enterprise Design
Patterns
Resources