Thursday, 25 February 2016

Apex Readable Code - Best Practices

Code readability is a universal subject for computer programming that should be learned as developers. This article is for Salesforce Developers and focuses on 8 most important best practices considered when writing readable code using Apex.
The eight most important practices are:
1.       Class Documentation - Every class/trigger should have proper documentation stating the purpose of class with its author.

2.       Commenting - Programmers use comments to annotate a program and help the reader (or grader) understand how and why your program works. Comments should describe what or why you are doing something, rather than how. Eg,
 i++;      //  increment i by one
·         Above comment how to increment rather why. So, do not write comments that restate the code.
·         Update the comments when you update the code.

3.       Consistent Indentation & Whitespace – When you are part of a team or if you are contributing code to a project, you should follow the existing style that is being used in that project.
·         Don't put more than one statement on a line.
·         Use blank lines to separate your code into logical sections.
·         Put a space between all binary operators (e.g., <=, =, +) and their operands. One possible exception is to emphasize precedence, like z = a*b + c*d
·         Put a space after each comma in an argument list.
·         Do not put spaces before a semicolon.
·         Do not put spaces between a method name and its left parenthesis.
·         Include blank lines to improve readability by grouping blocks of related code.
·         Avoid code horizontal line longer than 100 characters (approx., it depends).

4.       DRY Principle - DRY stands for “Don't Repeat Yourself”. The purpose for most applications (or computers in general) is to automate repetitive tasks. This principle should be maintained and same piece of code should not be repeated over and over again. Let say you need to check duplicate in a list on 2 VF pages or in 2 triggers then it’s good to have a static method in utility class which can be reused from anywhere within application.

5.       Overcome Apex Line Characters Limit
·         When populating map in apex in traditional style:

·         Populating map in apex, optimized style from 6 lines to 4 lines:

·         Use of ternary operator instead of if-else where possible,


6.       Consistent Naming - There are some general principles when choosing names for your variables, methods, and classes.
·         Use meaningful names that convey the purpose of the variable. Choose names that are easy to pronounce, and avoid cryptic abbreviations. For example, use wagePerHour or hourlyWage instead of wph. Use polygon instead of p or poly or pgon.
·         Be consistent.
·         Name boolean variables and methods so that their meaning is unambiguous, e.g., isPrime or hasValue.
·         Use shorter names (e.g., i) for short-lived variables and loop-index variables. Use more descriptive names for variables that serve an important purpose.
·         Avoid generic names like foo or tmp and meaningless names like fred. Use terminology from the application domain when possible.
·         Name a constant by its meaning, not its value, e.g., name your variable DAYS_PER_WEEK instead of SEVEN.
7.       Avoid Deep Nesting – Too many levels of nesting can make code harder to read and follow. 

For the sake of readability, it is usually possible to make changes to your code to reduce the level of nesting:

But my personal choice is method result variable and avoid multiple return in single method.


8.       Capitalize SOQL Special Words – It is a good idea to capitalize SOQL special words. Even though SOQL special words and function names are case insensitive, it is common practice to capitalize them to distinguish them from other variables and keywords.