Salesforce Code Scanner - How to Bake Quality in Your Code with Apex PMD
There are a series of best practices that Salesforce recommends that you follow when working on the technology. Whether you are an admin or a consultant doing the configuration or a developer writing code, these best practices help you build a solid foundation, improve quality, avoid making mistakes, and become better at what you do.
But it is also true that most of the time these best practices are not being followed. This may be because people are ignorant; following requires extra effort and/or extra investment in tools/technology.
If you are a Salesforce developer, here is one such tool that is free and easy to use and will guide you to bake quality into your code as you are developing it. After learning how to do it, there will be no reason for you not to use it in your day-to-day work and become an awesome developer. :-)
Called Apex PMD (stands for Programming Mistake Detector), this is a tool that analyzes source code to flag deviations from best practices, programming errors, bugs, stylistic errors, and suspicious constructs. You can perform Apex Static Analysis directly in Visual Studio Code on Apex and Visualforce files. Apex PMD is available as an extension for Visual Studio Code.
(If you haven't yet switched to Visual Studio Code for your Salesforce development work, you may want to refer to my step-by-step guide on "How To Get Started with Visual Studio Code for Salesforce")
Supported Actions
With Apex PMD, analysis can be performed when a file is opened or closed. It can be performed on a single file or on the entire workspace at once. You can also define your custom rulesets in .jar files to meet your code compliance needs.
Apex PMD is a very useful tool. You can refer to the suggestions/problems highlighted by the tool from the initial stages in the development cycle and make changes in your code accordingly. This will help you learn and follow the best practices right from the beginning, making you a better developer in the process with high-quality work.
Follow this guide and in less than 15 minutes (literally), you will learn how to install and use Apex PMD.
Here’s what this guide covers:
Install Apex PMD in Visual Studio Code
Perform Apex Static Analysis on a File
Perform Apex Static Analysis on the Workspace
Clear analysis problems from the Visual Studio Code panel
Set preferences for Apex PMD
After going through it, you will come to know that you can have a code companion to guide you in developing compliant, efficient, secure, scalable, and sustainable applications.
If you haven't yet started using Visual Studio Code for Salesforce development, I highly recommend starting to use it, as Salesforce itself has come up with an extension for VS Code and will continue to enhance it.
References & Useful URLs
Apex PMD Listing - https://marketplace.visualstudio.com/items?itemName=chuckjonas.apex-pmd
YouTube Video (73:08 minutes) - codeLive: Improve Apex Code Quality with PMD
Blog post - #How To Get Started with Visual Studio Code for Salesforce