function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RitikRitik 

APEX Best Practices

Hello,

What will be the impact/problem when

1. I f there  is a property which is get set and it is just plainly used in class but not on VF page.
2. property is initiallized but not used anywhere in class or Vf. (Impact in terms of speed of processing and memory allocation especially)
3. OrderBy Clause in terms of speed of processing (As I have a piece of code where OrderBy clauses are overused)
4. fieldName != null in where clause of SOQL



5. Please let me know what all should I look while reviewing an APEX code.
I am following https://developer.salesforce.com/page/Apex_Code_Best_Practices and few blogs searched from Google.


Regards,
Ritik

Best Answer chosen by Ritik
James LoghryJames Loghry

Hey, Pratik that's my article! :)

Ritik to answer your questions above:

  1. Preferrably you should adjust your variables pertaining to where they're accessed.  If my property is only used within the same Apex class, then it should be private, for instance.  If my variable is only set within my class (e.g. in the constructor), but it can be grabbed outside of the class, such as a VF page, then the getter should be public. If the class, method, or variable is part of a managed package, and then needs to be accessed outside of the package, then the access modifer should be global. But as a rule of thumb, I always keep my classes, variables, methods, classes with the least visibilty as possible, and if I need to refactor them it's usually not a big deal.
  2. If a property is initialized but not used, then why initialize / declare it in the first place?  Really, the impact depends on the initialization (or constructor of the object).  It may use up a bit of extra time to initialize the variable, but why do it in the first place if it's not used anywhere?
  3. Order By again depends on what field(s) you're ordering by.  If the field is indexed, such as an Id or External Id, or a custom index put in place by Support, then it will be faster than a non indexed field.  Also, the type of the field ordered has some impact (e.g. Number versus Text versus length of the field, versus how many combinations of that field there are in the Object.  In general, Order By will slow down your query because it has to sort through several records, but the question is how much of an impact it will have.
  4. In general you want to make your queries as selective as possible to improve performance, especially for large result sets.  Adding the != in your query makes it less selective, and will result in a less performant query.  Again, however, this is subjective and the time to run your query also depends on other factors.  For more info on selective queries, see: https://help.salesforce.com/apex/HTViewSolution?urlname=How-can-I-make-my-SOQL-query-selective-And-the-process-to-determine-the-fields-that-can-be-custom-indexed&language=en_US (https://help.salesforce.com/apex/HTViewSolution?urlname=How-can-I-make-my-SOQL-query-selective-And-the-process-to-determine-the-fields-that-can-be-custom-indexed&language=en_US)
Also, as Pratik pointed out the 15 Commandments article, I'm *hoping* to do another presentation on Apex Commandments this year at Dreamforce, so if you're going, plan on checking the session out.

- James

All Answers

PratikPratik (Salesforce Developers) 
Hi Ritik,

The link you mentioned is good. Also you can refer to:
https://developer.salesforce.com/blogs/developer-relations/2015/01/apex-best-practices-15-apex-commandments.html

Thanks,
Pratik
James LoghryJames Loghry

Hey, Pratik that's my article! :)

Ritik to answer your questions above:

  1. Preferrably you should adjust your variables pertaining to where they're accessed.  If my property is only used within the same Apex class, then it should be private, for instance.  If my variable is only set within my class (e.g. in the constructor), but it can be grabbed outside of the class, such as a VF page, then the getter should be public. If the class, method, or variable is part of a managed package, and then needs to be accessed outside of the package, then the access modifer should be global. But as a rule of thumb, I always keep my classes, variables, methods, classes with the least visibilty as possible, and if I need to refactor them it's usually not a big deal.
  2. If a property is initialized but not used, then why initialize / declare it in the first place?  Really, the impact depends on the initialization (or constructor of the object).  It may use up a bit of extra time to initialize the variable, but why do it in the first place if it's not used anywhere?
  3. Order By again depends on what field(s) you're ordering by.  If the field is indexed, such as an Id or External Id, or a custom index put in place by Support, then it will be faster than a non indexed field.  Also, the type of the field ordered has some impact (e.g. Number versus Text versus length of the field, versus how many combinations of that field there are in the Object.  In general, Order By will slow down your query because it has to sort through several records, but the question is how much of an impact it will have.
  4. In general you want to make your queries as selective as possible to improve performance, especially for large result sets.  Adding the != in your query makes it less selective, and will result in a less performant query.  Again, however, this is subjective and the time to run your query also depends on other factors.  For more info on selective queries, see: https://help.salesforce.com/apex/HTViewSolution?urlname=How-can-I-make-my-SOQL-query-selective-And-the-process-to-determine-the-fields-that-can-be-custom-indexed&language=en_US (https://help.salesforce.com/apex/HTViewSolution?urlname=How-can-I-make-my-SOQL-query-selective-And-the-process-to-determine-the-fields-that-can-be-custom-indexed&language=en_US)
Also, as Pratik pointed out the 15 Commandments article, I'm *hoping* to do another presentation on Apex Commandments this year at Dreamforce, so if you're going, plan on checking the session out.

- James
This was selected as the best answer
PratikPratik (Salesforce Developers) 
Hi James.

Nice article it is, will certainly attend the session if i am in of DF.

Thanks,
Pratik