Salesforce Development Tutorial: How to use the SFDX Scanner Salesforce CLI Plugin for Static Code Analysis

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!

Youtube
Patreon
Github
Facebook
Twitter
Instagram


Salesforce Development Books I Recommend

Advanced Apex Programming
Salesforce Lightning Platform Enterprise Architecture
Mastering Salesforce DevOps

Salesforce Development Tutorial: How to Setup Uncrustify to automatically format your code in Visual Studio Code

Why would you want to setup Auto Code Formatting?

Aside from the fact that it’s both magical and amazing, it also enables you to easily do a couple things:

1) If you get onboarded to a project with a horrific looking codebase with nightmarish formatting, you can fix that by running a single command on the entire thing to fix it. This saves you what would otherwise take dozens if not hundreds of hours of time

2) If you are on a large team of developers and you want your code to all look the same (at least on your commit to your codebase), you can create one code style configuration file and distribute it to the entire team for use. This way your code all looks and reads the same which makes everything just a little bit nicer to deal with.

There are other reasons but those are the two major ones I can come up with off the top of my head here, lol.


Why use Uncrustify for auto-code formatting?

I like Uncrustify for automatic code formatting because it has so many configuration options, 735 in fact! It allows you to configure how you like your code to look as opposed to many other popular auto-code formatting tools that are “opinionated” that don’t give you that flexibility.


How to Setup Uncrustify

To utilize Uncrustify with your Salesforce projects in Visual Studio Code you need to do the following (Github wiki is here):

1) Install Uncrustify locally on your machine. I would suggest installing it via a package manager like chocolatey or npm if you are using a windows machine (you may need to download npm or chocolatey if you haven’t already)

2) Install the Uncrustify Visual Studio Code plugin. Restart VS Code after installing it.

3) In Visual Studio Code press Ctrl + Shift + P to bring up the command palette and then type the following command: Preferences: Open Settings (JSON). This should bring up a JSON file with your VS Code settings.

4) Inside the settings.json file you opened in the last step, add the following lines:

"uncrustify.langOverrides": {
    "apex": "JAVA",
    "apex-anon": "JAVA"
},

"[apex]": {
    "editor.defaultFormatter": "LaurentTreguier.uncrustify"
 },

"[javascript]": {
    "editor.defaultFormatter": "LaurentTreguier.uncrustify"
}

5) Save your settings.json file

6) Press Ctrl + Shift + P to bring up the command palette again and enter the following command: Uncrustify: Create Default Config. After running this command you should see an uncrustify.cfg file in your VS Code project

7) Open the uncrustify.cfg file and update all your settings in it (This is just a nice UI with a ton of different options, so nothing complicated here). Make sure to hit the save button in the top right corner when you are done!

8) Once you finished open a file you would like to have code formatting done to and press Alt+Shift+F

And that’s it boiz and gurlz! Enjoy your auto-formatted code!


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!

Youtube
Patreon
Github
Facebook
Twitter
Instagram


Salesforce Development Books I Recommend

Advanced Apex Programming
Salesforce Lightning Platform Enterprise Architecture
Mastering Salesforce DevOps