Sub topic 1
What Code Analyzer does and why use it
Prerequisites
This chapter assumes you have:
- Basic Apex and either Lightning Web Components or JavaScript.
- The Salesforce CLI (
sf) installed and used from a terminal. - A Salesforce DX project layout with a
force-appfolder and ansfdx-project.jsonfile. - Some familiarity with Git and a basic CI workflow.
You don't need to run anything yet. Later chapters cover installing and running the tool.
What Code Analyzer is
Salesforce Code Analyzer is a static analysis tool. It reads your source files without deploying or executing them and reports patterns that are likely to cause bugs, security problems, or poor performance. You use it through the Salesforce CLI with the sf code-analyzer commands.
It covers the languages you write on the platform: Apex, Lightning Web Components, Aura, JavaScript, Visualforce, and Flows. It also checks the third-party libraries you bundle.
A finding in practice
Consider this Apex class:
public class AccountUpdater {
public static void renameAll(List<Account> accounts) {
for (Account acc : accounts) {
acc.Name = acc.Name + ' Inc.';
update acc;
}
}
}The code compiles and may pass a small unit test. With 200 or more records, though, the update statement inside the loop issues one DML statement per record. DML is the Apex term for database write operations. Apex limits the number of DML statements per transaction, so the class fails under real data volumes. A code reviewer can miss this. Code Analyzer's PMD rules flag it automatically as a governor-limit risk. The fix is to update the whole list once, after the loop.
Why use it
- Consistency. The same rules run on every change, whoever wrote or reviewed it.
- Early feedback. You find problems on your machine or in a pull request, before they reach a sandbox or production.
- Security coverage. Rules look for issues such as injection risks and vulnerable bundled libraries.
- Control. You choose which rules run, change their severity, ignore folders, and decide what blocks a build.
How the pieces fit together
Code Analyzer is a coordinator. It does not analyze code itself. It runs several engines, which are separate analysis tools. Each engine contributes its own rules, and Code Analyzer merges the findings into one report.
The next chapter describes each engine. Two ideas from this overview matter first:
- A rule is a single check, such as "no DML inside a loop." Every rule belongs to one engine.
- A violation is one place where your code breaks a rule. Each violation has a severity from 1 (Critical) to 5 (Info).
What it does not do
Code Analyzer does not replace tests, and it can't prove your code is correct. Some findings are false positives, meaning the code is fine but a rule flagged it anyway. You triage these by fixing the code or suppressing the finding with a justification. The rest of this tutorial shows how.
Where this tutorial goes next
You will install the plugin, list and select rules, scan a workspace, read the results, and configure the tool with code-analyzer.yml. Finally, you will make a CI job fail when violations reach a chosen severity.
Exercise
In the AccountUpdater example, why does a static analysis tool catch the problem even though no test fails? Name the kind of risk it reports.
Show answer
Static analysis reads the code without running it, so it can see that update acc; sits inside a loop no matter how small the test data is. Tests with few records stay under the DML limit and pass. The tool reports it as a governor-limit and performance risk, because one DML statement per record will exceed Apex limits at larger volumes.