Apex Code Best Practices

As with any programming language, there are key coding principles and best practices that will help you write efficient, scalable code.

Below are some point about apex best practices –

1) Apex is case in sensitive, for better readability use naming conventions like in Java

2) Bulkify your Code – Write the code handles a single record as well as more than one record at a time in case of batch.

3) Avoid SOQL Queries or DML statements inside FOR Loops – If you write a SOQL inside a for loop that may throw LimitException if it goes beyond 100 SOQL limit.

4) Bulkify your Helper Methods – Helper methods must take collection (Array, List, Set, etc.) as argument for bulk data processing.

5) Use of the Limits Apex Methods to Avoid Hitting Governor Limits – In systems debug log you can print different methods of Limit class like Limits.getLimitQueries() to check for governor limits

6) Use @future Appropriately – Keep in mind the governor limit of @future methods

7) Writing Test Methods to Verify Large Datasets – Write test case that handles large data

8) Avoid Hard-coding IDs – Try to avoid writing hard-coded ID inside apex code

Gopal Das
Follow me

Gopal Das

Founder at GopalDas.Org
He is a technology evangelist, Salesforce trainer, blogger, and working as a Salesforce Technical Lead. After working in Java based project implementation, he jumped to the Salesforce system on a whim and never looked back. He fell in love with Salesforce’s flexibility, scalability, and power. He expanded his knowledge of the platform and became a Certified App Builder, Administrator, Platform Developer I, SalesCloud Consultant while leading the Salesforce implementation and technology needs. He has worked in a wide variety of applications/services like desktop, web and mobile applications.
Gopal Das
Follow me

Leave a Reply

Your email address will not be published. Required fields are marked *