Code Review in Salesforce: Best Practices for Developers

July 13, 2018 Harish Khatri
 
Appirio’s solutions 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 than just making it work. In the end, it’s about making it work in the right manner. 
 
Also, good 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 on this topic. 
 
Why is code review important?
  • To find and fix errors while spotting performance issues throughout the code development process.
  • To maintain a level of consistency in design and implementation.
  • To identify common errors and share them with the team, thereby reducing rework and promoting understanding of the codebase across teams.
  • To build confidence among clients.
  • 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
The above trigger is only to handle first record in the Trigger.new rather than all records. And thus, should be bulkified.
 
Correct
In the below trigger, we will learn how to bulkify the trigger and how to use the collection.
 
Sample 2
Incorrect
The below trigger uses SOQL in a loop.
 
Correct
We should use SOQL outside the for loops to avoid SOQL governor limits.
 
Conclusion
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 that everything that’s delivered is perfect, and that our code review is one of the best practices that our developers follow in project management. Our team can help you with the latest cloud solutions including Salesforce Lightning Bolt and Warranty Management. Visit us at Appirio Hub to learn more about our innovative cloud solutions.
Previous Article
Salesforce CI/CD Using Bitbucket Pipelines
Salesforce CI/CD Using Bitbucket Pipelines

What if we could automate Salesforce Release Management using ANT and catch errors while deploying it to al...

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.