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

Leave a Reply

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