Code Review in Salesforce: Best Practices for Developers

July 13, 2018 Harish Khatri
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 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.

Blue three step flow chart illustrating the code review process


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
An incorrect apex code example in Salesforce that should only be used to handle the first record in
The above trigger is only to handle the first record in the rather than all records. And thus, should be bulkified.
A correct apex code example in Salesforce essential to the code review process
In the below trigger, we will learn how to bulkify the trigger and how to use the collection.
Sample 2
An incorrect apex code example in Salesforce to demonstrate the importance of code review
The below trigger uses SOQL in a loop.
A correct example of trigger in Salesforce using SOQL in a loop
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
Better Approaches for Attachment Migration to Salesforce
Better Approaches for Attachment Migration to Salesforce

Attachment migration can be a pain for migration specialists, but choosing the right approach can simplify ...

Next Article
React Native: The Good, The Bad, and The Ugly
React Native: The Good, The Bad, and The Ugly

A short, easy-to-read summary of the best and worst parts of developing with React Native.