When we are developing controllers and controller extensions that are associated to Visualforce pages, we need to adhere to the following best practices:
By using the
with sharing
keyword, we can enforce the sharing rules in controllers. Then the code will execute in the user mode instead of the system mode.We must not depend on the setter method to be executed before the constructor.
We must not depend on the execution order or side effects while creating custom methods in a custom controller or a controller extension.
Do not use DML operations inside a loop.
While performing record filtering, add filters in the following order:
In SOQL
In Apex
In Visualforce
If possible, calculations must be performed in SOQL instead of Apex.