Coding Checklist
1. Do not declare variables inside ‘for/while/do while’ loop.
2. When messages or constants are used at number of places define those as
constants at the top of code or define in one common file.
3. Check the variables/ values for ‘NULL’ when using QueryString arguments,
Request Parameters, SQL QueryResult etc. like:
if (Request[“id”] == null)
// handle and take some action
There may be cases where you just want to check and replace the null with a blank
value in that case put the following function in your “Common Functions” code file
as below:
// below function replaces the null with blank string also trims the passed string.
public String clearNull(String str)
{
If(str == null) return ‘’;
if(str == null)
Str = ‘’;
return str.trim();
}
4. Same variable declaration at number of places in same code file should be
avoided, avoid variable duplicity in same code file; Suggested that variable should be
declared once at the top of code
5. Variables which are used only once should be declared just before their use, means
declare the variable locally and no need to make the declaration at the top.
e.g. String strSql = “”;
strSql = “Select id, name from Account where id=”+acctId;
6. Variable names should be suggestive/ relevant.
7. DML queries not to be inside loop:
While writing Apex code, DML operations should not be performed inside any loop
as in that case you will run into Governor Limits and your program will fail.
8. SOQL queries not to be inside loop:
SOQL queries in the Apex code should not be inside the loop as in that case you will
run into Governor Limits and your program will fail.
e.g. create an array and use the ‘IN’ operator in the where condition of SQL query.
9. Handle the special characters specially ‘Single Quote’: to avoid SQL Injections
always handle the single quote.
In Apex you can use “escapeSingleQuotes” string function to escape the single quote.
In JavaScript use escape character backslash “” before any character you want to
escape like single quote.
When submitting any data containing “single quote” to SalesForce from your Client
Program or S-Control, replace “single quote” with “2 single quotes” and then submit
to Salesforce.
10. Create different function for each individual task
e.g. if program is doing 2 tasks like Insert and Update, create 2 different functions to
perform these 2 tasks.
11. Always put your code into try catch block, even if you are not expecting any
exception. Throw the exception to the calling program if you are not handling
exception in the catch block, or put a comment if you are not doing anything in the
catch block.
try
{
//some function call which throw an exception
}
catch(Exception ex)
{
// some code
//throw the exception to the calling program
throw ex;
}
try
{
//some API function call which MAY throw an exception
}
catch (Exception ex)
{
//put some comments like why not handling the exception or throwing to the
//calling program; e.g No need to do anything here, as program is not affected
by //the error thrown from the API function call
}
12. System.Debug statements
Define a Boolean constant and check the value of that constant to execute the
System.Debug() statements.
e.g.
// define below constant at top of your code or in one common static file
public static const boolean DEBUG = true;
// some code
if (DEBUG)
{
System.debug(‘write some message’);
}
Before releasing the code set the value of DEBUG constant as ‘false’;
Un-necessary System.Debug statements should always be removed before releasing
the code for review or deployment.
13. In JavaScript when you know that code processing should not continue may be
due to some condition, ‘return false’ from the innermost function to the outermost
function.
E.g
function func1()
{
//code
//code
if (some condition)
{
func2();
// if return is used as below, it will not go to the code after if completes
return func2();
}
// some more code
// some more code
}
function func2()
{
// code
//code
return false;
}
14. In S-controls, use merge global variables instead of SOQL queries where ever
possible
e.g. {!$Profile.Name}, {!$User.Username}, {!$Organization.Id}, {!
$UserRole.OpportunityAccessForAccountOwner}

Coding standards

  • 1.
    Coding Checklist 1. Donot declare variables inside ‘for/while/do while’ loop. 2. When messages or constants are used at number of places define those as constants at the top of code or define in one common file. 3. Check the variables/ values for ‘NULL’ when using QueryString arguments, Request Parameters, SQL QueryResult etc. like: if (Request[“id”] == null) // handle and take some action There may be cases where you just want to check and replace the null with a blank value in that case put the following function in your “Common Functions” code file as below: // below function replaces the null with blank string also trims the passed string. public String clearNull(String str) { If(str == null) return ‘’; if(str == null) Str = ‘’; return str.trim(); } 4. Same variable declaration at number of places in same code file should be avoided, avoid variable duplicity in same code file; Suggested that variable should be declared once at the top of code 5. Variables which are used only once should be declared just before their use, means declare the variable locally and no need to make the declaration at the top. e.g. String strSql = “”; strSql = “Select id, name from Account where id=”+acctId; 6. Variable names should be suggestive/ relevant. 7. DML queries not to be inside loop: While writing Apex code, DML operations should not be performed inside any loop as in that case you will run into Governor Limits and your program will fail. 8. SOQL queries not to be inside loop: SOQL queries in the Apex code should not be inside the loop as in that case you will run into Governor Limits and your program will fail. e.g. create an array and use the ‘IN’ operator in the where condition of SQL query. 9. Handle the special characters specially ‘Single Quote’: to avoid SQL Injections always handle the single quote. In Apex you can use “escapeSingleQuotes” string function to escape the single quote. In JavaScript use escape character backslash “” before any character you want to escape like single quote. When submitting any data containing “single quote” to SalesForce from your Client Program or S-Control, replace “single quote” with “2 single quotes” and then submit to Salesforce. 10. Create different function for each individual task
  • 2.
    e.g. if programis doing 2 tasks like Insert and Update, create 2 different functions to perform these 2 tasks. 11. Always put your code into try catch block, even if you are not expecting any exception. Throw the exception to the calling program if you are not handling exception in the catch block, or put a comment if you are not doing anything in the catch block. try { //some function call which throw an exception } catch(Exception ex) { // some code //throw the exception to the calling program throw ex; } try { //some API function call which MAY throw an exception } catch (Exception ex) { //put some comments like why not handling the exception or throwing to the //calling program; e.g No need to do anything here, as program is not affected by //the error thrown from the API function call } 12. System.Debug statements Define a Boolean constant and check the value of that constant to execute the System.Debug() statements. e.g. // define below constant at top of your code or in one common static file public static const boolean DEBUG = true; // some code if (DEBUG) { System.debug(‘write some message’); } Before releasing the code set the value of DEBUG constant as ‘false’; Un-necessary System.Debug statements should always be removed before releasing the code for review or deployment.
  • 3.
    13. In JavaScriptwhen you know that code processing should not continue may be due to some condition, ‘return false’ from the innermost function to the outermost function. E.g function func1() { //code //code if (some condition) { func2(); // if return is used as below, it will not go to the code after if completes return func2(); } // some more code // some more code } function func2() { // code //code return false; } 14. In S-controls, use merge global variables instead of SOQL queries where ever possible e.g. {!$Profile.Name}, {!$User.Username}, {!$Organization.Id}, {! $UserRole.OpportunityAccessForAccountOwner}