Improve Your Apex Code

Learning Objectives

After completing this unit, you’ll be able to:

  • Follow Salesforce-defined best practices for Apex code.
  • Apply the basic principles of test-driven development.

Best Practices

This unit presents rules, concepts, and guidelines defined over many years by Salesforce developers. Based on their collective experience, we can safely say that adhering to these principles improves software development. There's a lot of ground to cover, so we'll be short and sweet.

Apex

The first rule of Apex is: You do not use Apex. Not when you don't need to. First, try to customize Salesforce by using the platform's built-in features, such as workflows, validation rules, and process builders. If you must use Apex code, aim for an 80:20 ratio of configuration to code.

Other best practices for Apex include:

  • Bulkify your data manipulation language (DML) statements and use the built-in Limits methods. For more information about Limits methods, see the resources at the end of this unit.
  • Write comments with a good balance between quality and quantity (explain, but don't over-explain). Always provide documentation comments (method and class headers). Provide inline comments only when necessary to clarify complex code.
  • Avoid nesting loops within loops.
  • Avoid SOQL queries and DML statements inside loops.
  • Keep logic out of triggers. (Instead, use a trigger framework.)
  • Create relationships between objects to reduce your number of queries.
  • Avoid hard coded IDs and constants.
  • Increase readability by indenting code and breaking up long lines.
  • Break methods into multiple smaller methods if possible, making your code more modular and easier to read and maintain.
  • Last but not least: adhere to the security guidelines covered in the Develop Secure Web Apps trail. This includes declaring a sharing keyword for every class, and making efforts to prevent XSS and SQL injection.

For more information on these and other best practices, see Apex Best Practices: The 15 Apex Commandments in the Resources section. If you are using Visual Studio Code as your IDE, consider using the Apex PMD extension (included in the Resources section) to automatically check your code for most best practices.

Test Coverage

When you write great code, you want to ensure that it doesn't interfere with your other great code, or get broken by great code you'll write in the future. That is why developers write test classes. The Salesforce documentation offers many test coverage best practices, so we won't reiterate those here. (For more information, see the Resources section.) What we will do, however, is encourage you to write your test classes as part of a process called test-driven development.

Test-driven development (TDD) reverses the traditional development process, in which you first write your code and then the test class. In TDD, each new user story or feature starts with writing a test class that defines the desired behavior. You then write the minimum amount of code needed to pass that test. Finally, you refactor the code so that it adheres to best practices.

TDD leads to more modularized, flexible, and extensible code. The structured testing process makes it easier to verify whether you're delivering code that satisfies the statement of work, especially when requirements change during the development process. Automated test cases help others to understand what your code is supposed to do, which is important when other team members debug or extend your work. In other words, TDD is a great way to write test coverage that tests not just your code's quality, but also its real-world impact.

Start Using Coding Conventions Today

This module took you on a journey through the wonderful world of coding conventions. You learned how naming conventions can clarify what your code does and how it relates to other code. You can now use frameworks to develop code more efficiently and deliver higher quality code by adhering to best practices. 

Knowing about coding conventions is one thing, but using them is another. Only when you actively use coding conventions can you discover their true power. It takes just one Trailblazer to transform a project, and with the knowledge that you have now, you can be that person. Be the one who steps up and makes a change. Trust us. Your team will be grateful.

Resources

Keep learning for
free!
Sign up for an account to continue.
What’s in it for you?
  • Get personalized recommendations for your career goals
  • Practice your skills with hands-on challenges and quizzes
  • Track and share your progress with employers
  • Connect to mentorship and career opportunities