Code Review in Salesforce: Best Practices for Developers

July 13, 2018 Harish Khatri
Image depicting blue data and what is code review in Salesforce
 
Appirio experts develop small- to large-scale solutions, depending on customer needs. However,  solutions that work for the same functionality are not similar in quality. There are various aspects to developing a code, aside from just making it function. In the end, it’s about making it work right. 
 
Quality code should be easily managed (easy to understand and modify by other developers), while also being robust and secure from various vulnerabilities and exploits. This rings especially true within Salesforce, where we have a lot of governing limitations. This is where code review stands in a project lifecycle, to ensure the quality of deliverables to the client.
 
So, let's have a look at the various aspects of code review to gain a better understanding of this topic. 
 
Code review in Salesforce (and in general): Why it matters...  
  • Helps find and fix errors and spot performance issues throughout the code development process.
  • Maintains a level of consistency in design and implementation.
  • Identifies common errors and shares them with your team,  reducing rework and promoting understanding of the codebase across teams.
  • Build customer trust and confidence. 
  • Reduces the testing turnaround time.
Key elements of code review
  • When: When developers have finished the task and marked it as ready for review.
  • Who: The developers and reviewers.
  • What: The entire code is reviewed, following the coding standards, and then feedback is given.
 

 
Common checklist for the Reviewer:
  • Check for Bulkification in the code
  • Avoid SOQL Queries inside FOR loops
  • Implement Collections wherever possible 
  • Avoid multiple triggers on the same object
  • Avoid hard coding IDs
  • Avoid lengthy functions and classes
  • Use standard Apex SOQL format
  • Check for NULL pointer exceptions
  • Use Custom Settings, Custom Metadata or Constants class to store static data
  • Never use custom labels for storing static data
  • No queries and DML inside getter methods
  • Use Custom Labels for frontend messages
  • Use relative comments and appropriate naming conventions in your code
 
Some Apex Code Examples in Salesforce (SFDC) 
 
Sample 1
Incorrect
Image of which code review steps to follow
The above trigger is only to handle the first record in the Trigger.new rather than all records. And thus, should be bulkified.
 
Correct
Image of how code review should be done
In the below trigger, we will learn how to bulkify the trigger and how to use the collection.
 
Sample 2
Incorrect
Image of how code review works the wrong way
The below trigger uses SOQL in a loop.
 
Correct
Image of how code review should be done
We should use SOQL outside the for loops to avoid SOQL governor limits.
 
Final thoughts on code review best practices... 
As we know, code plays an important role in determining project health. The quality of the code is a significant factor we need to take care of while implementing the project.
 
We want you to have the best practices in place while implementing Salesforce solutions. Our Salesforce experts make sure to deliver high-quality reliable code. They also encourage code throughout an entire project's lifecycle. Our amazing team can help you with the latest cloud solutions, including Salesforce Lightning Bolt and Warranty Management.
 
Visit us at the Appirio Hub to learn more about our innovative cloud solutions.
Previous Article
Creating a Journey from Scratch
Creating a Journey from Scratch

We've put together four easy steps to creating a Journey from scratch in Salesforce Marketing Cloud.

Next Article
Retail Banking Discovers the Power of Salesforce’s Financial Services Cloud
Retail Banking Discovers the Power of Salesforce’s Financial Services Cloud

Many recognized banks have started implementing Salesforce's Financial Services Cloud. Find out how it can ...

Are You Ready for Salesforce Lightning?

Read More