Apex Code size limitsYou 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.
AlgorithmYou 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 librariesLeverage built-in features of Apex code instead of rewriting your own. Math class, crypto... 1 line of code instead of lots of code.
Avoid using temporary variablesIt takes more characters, operations and memory to write
Shorten variable declarationsYou can combine variable declaration:
Shorten variable nameYou 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 NameIf 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 structureFor our demonstration, we will create a new Class named CodeSize.
After having created this blank class using Developer console, size equals 24 chars
Let's add a comment to identify impact
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:
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:
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 debugssystem.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 bracketingWhat is the difference between this
ConclusionWith 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.