Apex Best Practice Flashcards
1
Q
Code Commenting
A
- We can reduce the amount of comments we have to write be creating self-documenting code (Code with descriptive method/variable names that allow others to infer their purpose from the name alone)
2
Q
Testing
A
- Even though we have a test coverage requirement of 75% for Apex code, hitting this floor does not mean that we’ve written good, meaningful tests that will ensure that our code works as expected
- To make sure we’ve created good, meaningful tests, we should adhere to our testing best practices (test single, bulk, positive, negative, and as a restricted user)
3
Q
Modularity
A
- Writing modular methods (i.e. one method
for each piece of business logic such as one
method to handle SOQL queries) is a good
practice to implement - We can also achieve modularity by moving
repetitive code to a single method that can
be invoked multiple times
4
Q
Bulkify
A
- Design code that can handle large amounts of records/data
5
Q
Staying within Governor Limits
A
This can be done by: - Avoiding SOQL/DML in loops whenever possible - Operating on collections of records rather than individual records - Invoking asynchronous Apex if we still think we might run into governor limits after implementing all of the above
6
Q
Follow Trigger Best Practices
A
- 1 trigger per object, we can’t guarantee the
order our triggers execute on the same
object ahead of time - Make logicless triggers, move all logic to
helper/handler method and call that method - Avoid Recursive triggers
- Bulkify
7
Q
Don’t hardcode Record Ids
A
- There’s no guarantee that the same record will have the same Id in two different orgs
- Additionally, avoiding hardcoding is a good general principle to have - the more dynamic our code, the better