Apex code quality describes how secure, efficient, readable, and maintainable the Apex classes and triggers in an org are. Code that compiles and passes tests can still query inside loops, ignore sharing and field security, swallow exceptions, or duplicate logic across classes. Static code analysis finds these problems by reading the source code without running it, so teams can fix issues before they cause failures, security exposures, or governor limit errors in production.
Common Apex Quality Problems
- Performance. SOQL queries and DML statements inside loops, which hit governor limits as data grows.
- Security. Code that does not enforce sharing or field-level security, and queries built from unescaped user input.
- Error handling. Empty catch blocks and unhandled exceptions that hide failures.
- Design. Very long methods, excessive complexity, and logic placed directly in triggers.
- Dead and duplicate code. Classes and methods that nothing calls, and copies of the same logic in several places.
How to Approach Code Quality
- Scan all classes and triggers with a static analyzer to establish a baseline.
- Prioritize rules. Decide which rules are mandatory for your team, such as security rules, and which are suggestions.
- Fix the highest priorities first, and review every change, including AI-proposed changes, for unchanged business behavior.
- Remove dead code. Unused classes and methods add maintenance cost and count toward the org's Apex size limit.
- Gate releases. Check quality before deployment and alert when new high-priority findings appear.
How Snapshot Helps
- Apex Code Quality Report: uses PMD static analysis across Best Practices, Code Style, Design, Documentation, Error Prone, Performance, and Security rules. Manage Quality includes rules and sets Mandatory, Required, Recommended, Suggested, or Optional priorities. Reports show rule matches, affected lines, and documentation links. The Editing Palette offers Manual Editing, Quality Improvement, and Multiple Improvements for AI-proposed changes to review before Deploy, and Schedule Report supports priority-based alerts.
- Metadata Quality Report: editable AI checks for content problems such as hard-coded logic, across metadata types beyond Apex.
- Forgotten Assets Report: finds duplicate and unused Apex methods.
Static findings reflect the included rules. Findings do not establish runtime failures, security defects, test coverage, or correct behavior.
Metadata Types Involved
-
ApexClass: Apex classes, including controllers, services, and tests. -
ApexTrigger: code that runs on record events.
Common Questions
What is PMD?
PMD is an open-source static code analyzer with rule sets for Apex. PMD reads source code and reports rule violations without running the code.
Does passing static analysis mean the code is correct?
No. Static analysis finds patterns that are risky or poorly structured. Tests and reviews still confirm that the code does what the business needs.
Should business logic live in Apex triggers?
A common practice is to keep triggers thin and call handler classes, which makes logic easier to test, reuse, and order.