Wednesday, February 27, 2013

Best Practices For Salesforce Coding

  • Use header comments on top classes as below:

      /**
        @ name                      :
        @ author                    :
        @ date                      :
        @ description               :
        @ VF page Name              :
      */

  • Variable/method names needs to be in camelCase as possible. Like creatCase(), getClosedWonOpportunity() etc.

  • Variables names must be meaningful as possible, don't like x, y etc


  • Use curly braces with same line instead of next line in if/else and loops constructs as below:
       if(bla bla){

       }

  • Comments at on every possible complex logic, on top of each methods also about its working.


  • Keep method as possible small , if going logic more than 50 rows, then break that into other method.


  • Test classes must be meaningful, means must have asserts of the main logic, must have coverage near to 80% as possible (at-least 75% is required), use separate test class to cover each separate apex class.


  • Use proper Oops concept as possible , means use private where ever possible, extends, method overriding , method overloading etc.


  • Don’t hard code ids and any hard code values in code, use query to fetch data or in test classes make new objects.


  • Don’t hard code message or error text, try to use Label as possible (If client says).


  • Use components as possible for header, footer in pages.


  • use standard apex tags as possible.


  • Whenever possible make your Utility class which has useful, re-used methods and put your methods in that and use that class in every class logic.


  • If any trigger logic is very big, then try to make one class and put your logic in that class and call that class in your trigger. Keep trigger small and as simple as possible.

  • Check if condition before using the list. eg: 
     if(ContactList.size() > 0)
     {bla bla}

  • Avoid Nested loops, which may cause maximum script statements governor limits to be hit. Should make use of Maps to avoid nested loops.


NOTE: The aforementioned Best Practices are according to me. Salesforce itself output their own Best Practices. I have cited and summarized some of the excerpts it.

9 comments:

  1. The applications on Force.com platform using Apex, I am sure you may have been in need of debugging your code. Salesforce being cloud platform, method to debug code is very different as compared to other programming languages. In this post we will discuss all about Apex code debugging, challenges, solutions and other best practices.

    salesforce training in chennai

    ReplyDelete
  2. I am obliged to you for sharing this piece of information here and updating us with your resourceful guidance. Hope this might benefit many learners. Keep sharing this gainful articles and continue updating us.
    Salesforce Training in Chennai
    German Classes in Chennai
    Salesforce certification Training in Chennai
    Salesforce.com training in chennai
    best german classes in chennai
    German language training in chennai

    ReplyDelete
  3. Congratulations on your article, it was very helpful and successful. 57eeaddb602baac8c865425e7ed85a32
    website kurma
    numara onay
    sms onay

    ReplyDelete
  4. Thank you for your explanation, very good content. aae4fd5f054b5b6a1222d9ae457fcfe2
    altın dedektörü

    ReplyDelete