SlideShare a Scribd company logo
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}

More Related Content

PPTX
Java SE 11 Exception Handling
PPT
C# Exceptions Handling
PPTX
Cold fusion best practice
PDF
AngularJs Style Guide
PPTX
Cordova training : Day 3 - Introduction to Javascript
PPTX
Exception handling in ASP .NET
PDF
Exception handling in plsql
PPTX
Exception handling in asp.net
Java SE 11 Exception Handling
C# Exceptions Handling
Cold fusion best practice
AngularJs Style Guide
Cordova training : Day 3 - Introduction to Javascript
Exception handling in ASP .NET
Exception handling in plsql
Exception handling in asp.net

What's hot (17)

PDF
Zero values in Priority tables - a blessing and a curse
PPTX
LISP: Errors In Lisp
PDF
Best Practices in Exception Handling
PDF
Java input Scanner
PPT
Week7 exception handling
PPTX
Function Returns
DOCX
Cis 355 i lab 1 of 6
PPTX
Functions
PDF
Java Repetiotion Statements
PPTX
Function Parameters
DOCX
Introduction to c programming
DOCX
Cis 355 i lab 1 of 6
PDF
OOPS With CSharp - Jinal Desai .NET
PPTX
Error handling and debugging in vb
PPT
Exception Handling Java
PPTX
JavaScript Functions
PPT
Md04 flow control
Zero values in Priority tables - a blessing and a curse
LISP: Errors In Lisp
Best Practices in Exception Handling
Java input Scanner
Week7 exception handling
Function Returns
Cis 355 i lab 1 of 6
Functions
Java Repetiotion Statements
Function Parameters
Introduction to c programming
Cis 355 i lab 1 of 6
OOPS With CSharp - Jinal Desai .NET
Error handling and debugging in vb
Exception Handling Java
JavaScript Functions
Md04 flow control
Ad

Similar to Coding standards (20)

PPS
Programming in Arduino (Part 2)
PDF
1669958779195.pdf
PPTX
3. Java Installations ppt for engineering
PDF
Comprehensive JavaScript Cheat Sheet for Quick Reference and Mastery
PDF
JavaScript Cheatsheets with easy way .pdf
PDF
svelte-en.pdf
PPTX
Final requirement
PPTX
SUBHASH.pptx
PPTX
Templates and Exception Handling in C++
PDF
Android Application Development - Level 3
PPT
Lecture 1
PPT
Lecture 1
PPT
PPT
SoftTest Ireland: Model Based Testing - January 27th 2011
PDF
iOS best practices
PDF
Describe's Full of It's
PPTX
Frontend training
PPTX
Loops in java script
PPTX
Exception
PDF
Java unit 11
Programming in Arduino (Part 2)
1669958779195.pdf
3. Java Installations ppt for engineering
Comprehensive JavaScript Cheat Sheet for Quick Reference and Mastery
JavaScript Cheatsheets with easy way .pdf
svelte-en.pdf
Final requirement
SUBHASH.pptx
Templates and Exception Handling in C++
Android Application Development - Level 3
Lecture 1
Lecture 1
SoftTest Ireland: Model Based Testing - January 27th 2011
iOS best practices
Describe's Full of It's
Frontend training
Loops in java script
Exception
Java unit 11
Ad

More from mallareddy0107 (9)

DOCX
Approval process
DOCX
Interview questions
DOCX
Interview questions
RTF
Hr interview
DOCX
Governor limits
DOCX
Data migration
DOCX
Sfdc Knowledge
DOCX
Approvals and workflow in salesforce
DOCX
Approval process
Approval process
Interview questions
Interview questions
Hr interview
Governor limits
Data migration
Sfdc Knowledge
Approvals and workflow in salesforce
Approval process

Coding standards

  • 1. 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
  • 2. 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.
  • 3. 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}