Thursday 31 March 2016

10 Commandments of Force.com Programming

Howdy Force.com programmers? Programming in a multi developer ecosystem like Force.com is nothing less than a band of musicians playing in an orchestra. Different instruments produce vivid tones, but exist in harmony, churning out a beautiful melody. Similarly, keeping the originality of each developer intact, yet ensuring the code adheres to common basic protocols, is of prime importance. Adding a method to the madness is required here!

After going through multiple articles concerning the best practices around Force.com programming, I deduced that although a lot of those struck the right chord, some, however, seemed to lack practicality while writing code.

This led me to the idea of writing of this blog to share an exhaustive checklist that ensures uniformity through basic standards  and is practical to use while programming on Force.com.

I call them the "Ten Commandments of Force.com Development" and here they are....


#
Thou Shalt...
Practice
1
NOT CODE unless it is really required. Use Workflows, Approval process, Validation Rules, Roll up Summary fields, Formula fields, and Process builder etc. to accomplish the requirement.
2
Always ensure general coding sanity.
  1. Don’t hard code IDs or String values. Use custom settings, Constants (static final variables) or Custom Labels instead.
  2. Always check for nulls within code. Use String.isBlank() or  list.isEmpty().
  3. Use String.equalsIgnoreCase() instead of equality operators(== or ===).
  4. Use meaningful naming conventions for variable, class, test class, trigger and page names. e.g. (MAX_ATTEMPTS, isActive, commissionControllerTest, AccountTrigger, viewCommissions).
  5. Document your code using class and method level comments.
3
Not use SOQL statement or DML operation within loops. Always use Lists, Sets, Maps to your code. Use SOQL for loops wherever applicable.
4
Always use nested or Inner SOQL queries to fetch parents and associated child records Use selective SOQL Queries using appropriate WHERE clause, filters on indexed fields (Name, Id, Unique fields) to fetch appropriate result set optimally.
5
Ensure creating only one trigger per object Use the Trigger Handler Framework to ensure execution control, Trigger Context Variable, Recursion checks.
6
Optimize Visualforce page view state for performance. Use transient variables, minified Javascript files, Streaming API, Standard Set controllers, and remove unused variable.
7
Always ensure error logging and reporting within your code. Use try, catch or email to catch and report errors within your code.
8
Always enforce security in code. Use with sharing, FLS checks, CRUD checks, Escape URL parameters to ensure authorized access.
9
Always write unit tests based on test cases. Use Test Data creation framework along with the @isTest, test.startTest() test.stopTest(), System.assertEquals(),bulk test data, different users / Profiles, WebServiceMock, 90% code coverage and check for limit uses.
10
Always ensure modularity and reusability in code
  1. Refactor method having > 45 lines of code into smaller methods, merger multiple line of code into a single line.
  2. Remove unused variables, System.debug(), alert().

In addition to using these 10 Commandments as a guideline, Force.com programmers performing code review can use this as a tool to patrol their Force.com implementations.

Happy Coding and “May the force be with you!!”



Written by Parmanand Yadav, Salesforce Champion at Eternus Solutions

1 comment: