Why Use Static Code Analysis Tools?
There are two major reasons to use static code analysis tools:
1) They make you a better developer. These industry standard rules exist for a reason. To make code clean, modular and easy for all the developers that come after you to deal with. So be a kewl guy and make some beautiful code.
2) If you haven’t had to deal with this, I envy you, but for many of us out there you will eventually have your codebases reviewed by a third party. It may even be Salesforce themselves, and when they review your codebase they will run these tools against it to gauge the health of the code in your org. I have seen people lose contracts because of this (and in some cases rightfully so), but if you are constantly running a static code analysis tool again your code you write every sprint and fixing the problems it finds, you’re not gonna see too many issues pop-up when someone else reviews the code.
How to use the SFDX Scanner Plugin?
To utilize the SFDX Scanner plugin we need to do the following (Checkout the GitHub wiki I wrote here):
1) Install the Java Development Kit (JDK) version 8 or higher
2) Install the Salesforce CLI
3) Setup a Salesforce VS Code Project
4) Install the sfdx scanner plugin for the Salesforce CLI by opening up a terminal window and running the following command:
sfdx plugins:install @salesforce/sfdx-scanner
5) Open the Terminal in VSCode and run the following command to scan your entire codebase for all static code issues (make sure to replace pathToFile.csv with the actual path for your file):
sfdx scanner:run --target "**/default/**" --format "csv" --outfile "pathToFile.csv"
6) The above command will output a csv that shows you all of the issues with the code in your org within a few seconds (unless you have a mega code-base. It may take a few minutes then).
7) For more information on the wealth of configuration options you have with this cli plugin, please check out the SFDX Scanner GitHub repo
That’s it guys! Pretty simple! There are a ton of additional configuration options you can do with this CLI plugin and you can check them out in the GitHub repo linked above. In a future article I will hopefully show you how to make custom pmd rules and rulesets to use if you aren’t a fan of the default ones, but that’s a pretty big topic.
Get Coding With The Force Merch!!
We now have a redbubble store setup so you can buy cool Coding With The Force merchandise! Please check it out! Every purchase goes to supporting the blog and YouTube channel.
Get Shirts Here!
Get Cups, Artwork, Coffee Cups, Bags, Masks and more here!
Check Out More Coding With The Force Stuff!
If you liked this post make sure to follow us on all our social media outlets to stay as up to date as possible with everything!
Salesforce Development Books I Recommend
Advanced Apex Programming
Salesforce Lightning Platform Enterprise Architecture
Mastering Salesforce DevOps