Apex Best Practices

Apex Best Practices:
1. All constants should have uppercase names, with logical sections of the name separated by  underscores.

2. Always check the returned values from Database.delete(), Database.emptyRecycleBin(),                 Database.insert(), Database.undelete(), Database.update() and Database.upsert() calls. If a list is passed to any of these methods, the call will return an array[] of results; the apex code must check all the return values in the returned array of results. 


3. Classes that contain methods or variables defined with the webService keyword must be declared as global.

4. Declare instance variable as private or protected.

5. Declare only one variable per line of code.

6. Define meaningful numbers using clearly named constants (static final).

7. Do not instantiate any object before you actually need them. Specifically outside a try-catch block.

8. Each method name should have meaningful names starting with lowercase and the following words should have initial uppercases.

9. Getter and Setter methods of class variables should have the get and set keywords followed by the variable name, with the first letter of the name capitalized.


10. Never Hard-code Ids.
                                                                    Wrong way:


                                                                Correct Way:

11. If a method, variable, or inner class is declared as global, the outer, top-level class must also be declared global.

12. Initialize all Boolean, Integer, Decimal, Double, and Long variables, instances, class and method scope at the point of declaration to a non-null value.

13. The method must not have no more than 2 levels of looping or other control structures.

14. All methods should have try-catch-finally blocks to handle possible exceptions.

15. Never hardcode an id, link, etc in code; instead use custom labels or Static Apex classes with constants (public static final) or enums.

16. Precede each isEmpty() method call with a null check.


17. Should initialize all variables before using them. Apex initializes all variables to null by default.

18. To convert a non-string variable to a string, use String.valueOf(variableName) instead of variableName + “ ”.



19. Use Sets, Lists, and Maps when returning data from Database, this makes your code more efficient because the code makes fewer trips to the database.

20. Use the Page.getUrl() method to get the URL/URI for a salesforce page.

21. When comparing a string literal to a string object, always use the syntax ‘String literal’.equals(stringObj).

22. All apex classes must have a class header, similar to the following at the start of every class. The header must use the Javadoc syntax.



23. All apex methods must have a header similar to the following.


24. All system.debug() statements should be removed before pushing code.

25. All unnecessary //comments removed before pushing code.

26.No switch/case OR if/then/else statements are allowed.

27. Always write SOQL queries outside for loop, make sure methods inside for loops do not have                SOQL queries as well.

28. Check all queries used in the code are strictly necessary and no further optimization can be done.

29. Always move repetitive code to a method.

30. Instead of storing a query in a list and Iterating over for loop, directly use the query in the condition of the for each loop, so it iterates directly over the query. 



31. Combine If Statements wherever possible.

32. Executable statement count cannot exceed 30 for each method (Braces, comments, and blank lines are not included in the limit of 30).

33. Don’t send parameters to a method if they can be created within that method.

34.Use keyset to iterate.

35. Avoid SOQL injection.

Wrong way:


Right way:



36.Use isEmpty() instead of size() for a List, this increases the performance.

37. Use “.equallsIgnoreCase” instead of “==” when comparing Strings.

38.No need to check for “==true” for Boolean Variables.

39. Instead of specifying all the fields in the Query, use schema methods to iterate and build query sting. In the future even if there is a new field added, the code will be automatically handled.

40. To convert a list to a set simply use Set.addAll(List) method.









Comments

Popular posts from this blog

What are Duplicate Rules and Matching Rules? How to Invoke Duplicate and Matching Rules in Salesforce without performing DML operations?

LWC Best Practices