Best practices identify that the procedure followed is correct and acceptable for everyone. It is appreciated in both professional and commercial procedures. When it comes to coding or I can talk precisely about Salesforce best practices then one needs to keep in mind that it is a necessity. It's always better to follow discipline wherever we code no matter in which language we choose to code. Salesforce is the combination of both configuring the application and developing code. Multi-tenancy comes along with some restrictions and limits without affecting too much freedom to code in your own org. If the project follows the best practice then there will always be less rework after reviewing.
Don’t forget to check out: How to get Salesforce coding experience?
- As there is already much standard functionality present and available, it's better to ‘adopt’ them and carry out the need.
- Only go for custom development when it's impossible to meet the business requirement through standard one.
- Advisable, more adopt before adapt.
- Keep it simple, as complex code logic has expansive maintenance.
Now lets tour in various sections where we must be careful about what to follow and keep in mind where not to cross governor limits:
- Use comments to make it more understandable.
- Keep it less complex and better make it easily maintainable.
- Never hard code IDs or email addresses. Make it dynamic.
- Use debug logs and ensure its consistency.
- Code for performance.
- Don’t let the code have commented code, make it clean.
- It must have the same behavior while running and while testing.
- Naming conventions should always be taken into consideration.
- Avoid writing DML inside loops.
- Use bulkified DML operations to get a list of records.
- Always write one trigger per object or per event.
- Pass the logic in handler class and then code the trigger using that handler class.
- Remember that trigger handles 200 records per execution.
- Always optimize the trigger but do not allow more memory consumption while using indexed data type for the sake of optimization.
- Dependencies and impact should be managed like while naming the page.
- URL parameter must be handled properly as it must be passed in the correct format.
- Avoid using ViewState to make the performance better.
- Never write a test class without starting it with ‘@isTest’.
- For negative and positive, always put assert statements.
- Avoid using hardcoded IDs.
- It must have 75% coverage but always try to increase it to 95%.
- Keep the real scenario in mind while writing the test class as it will handle any number of records.
- Each branch of conditional logic must be tested.
As Salesforce follows multi-tenant architecture so it provides some limitations known as governor limits which restrict users or clients to overuse the resource at a time which alternatively affects any other user experience. To overcome such a type of situation, a limitation is required and to handle the use of resources for multiple users.
The best practice prevents the developers from crossing the governor limits. It's simple that if it is according to acceptance healthy then it is in limits. To maintain the less rework after the review and to avoid hitting the governor limits, then follow the rules early only. Get the precaution first to work smartly.