Apex Code size limits

You can write Apex code as long as you stay in the following default limit: 3 Mb of code excluding comments and @isTest classes. Even if it is possible to ask Salesforce to increase this limit, it is recommended to stay with low quantity of code.
Here are a few tips to reduce your code and stay within the quota.

Algorithm

You can often achieve the same result with less code if you think about the algorithm. Don't split in functions the parts of your algorithm as you can generally inline them, when used only within this code.
Use Lists and Maps for the right purpose, this is a good accelerator too.

Built-in libraries

Leverage built-in features of Apex code instead of rewriting your own. Math class, crypto... 1 line of code instead of lots of code. Decimal smaller math.min(ij);

Avoid using temporary variables

It takes more characters, operations and memory to write

List<AccountaccLst=[Select id from Account Limit 10];
for(
Account a:accLst){}

than to write

for(Account a:[Select id from Account Limit 10]){}

Shorten variable declarations

You can combine variable declaration:

Integer i 0,j=1;

Instead of

Integer i 0;
Integer j=1;

Shorten variable name

You can spare characters when defining a numeric counter as "i" instead of "myCounter". Try still to keep a meaningful name for business purpose (ex for an Account Number : String accNum;)

Shorten Field API Name

If the API name is long, it will have lots of code size impact : in the SOQL query size, in the code to use the field, and each time you need to access it.

Managing line structure

For our demonstration, we will create a new Class named CodeSize.

public class CodeSize {

}

To verify that we can improve the size with structural tips, we will query the ApexClass object using SOQL: Select LengthWithoutComments from ApexClass where Name='CodeSize'
After having created this blank class using Developer console, size equals 24 chars
Let's add a comment to identify impact

public class CodeSize {
//My comment
}

Size is still 24.
If we indent the comment with 2 spaces, what is the impact? Still 24
If we add a blank line ? no impact
Let's add a variable definition indented with 4 blanks:

public class CodeSize {
    
Integer i=0;
}

Size is 41. If we change i=0 into 0 (with space) size is 43. Blank space matters.
Add a space at the end of the line : size is 44: for any line of code, blank space is counted anywhere (indentation, syntax and end of line).
Now add a comment: Integer i 0//test
Size is 44 because comment does not matter. If you want to keep space between the code and the comment, it is better to write the comment in a dedicated line, in that way you won't have any blank space after the code that will count for the code length.
If we indent the variable definition with 6 spaces instead of 4, length of code becomes 46 bytes. Indentation matters.
If we replace the space indentation with a unique tab indentation, the length drops to 41.
Adding an empty line after the variable definition, with a mix of white spaces and tabs: no impact
Adding multi line comments /* xxx */ has no impact, even if indented. Blank space does not count for lines of comments.

System debugs

system.debug(...) does count against code length as it is not a comment. Use them during the development process, and remove most of them (keep only statements that are really usefully) for production-ready code.

Code bracketing

What is the difference between this

        if(1==2)
        {

        }

And this ?

        if(1==2) {

        }

9 characters: 8 for the space indentation and 1 for the end of line (new line). You should keep brackets on same line instead of new lines. This will give a readability benefits too as it will reduce the vertical scrolling of your code.

Conclusion

With all these findings, you will be able to define your own naming conventions to keep your code small, readable and efficient. Tests on various production orgs demonstrated that you could easily gain more than 10% on existing code by managing indentation (using tabs) and removing trailing white spaces on the line of code (this can be automated). Putting in place the other rules will finally reduce in a significant way the Apex length.


 
© 2018 Jean-Luc Antoine, All Rights Reserved