---
title: Apex General Best Practices
description: "This article is based on the PMD documentation article. See the original article on the PMD site: [PMD: best practices](https://pmd.github.io/latest/pmd_rules_apex_bestpractices.html). Below are the generally accepted best practices.  ## ApexUnitTestClassShouldHaveAsserts Apex unit tests should include at least one assertion. This makes the…"
url: "https://community-staging.qualityclouds.com/qcdocs/qcr/rules/apex-general-best-practices/"
updated: "2025-03-21"
category: Rules
---

# Apex General Best Practices

This article is based on the PMD documentation article. See the original article on the PMD site: [PMD: best practices](https://pmd.github.io/latest/pmd_rules_apex_bestpractices.html).

Below are the generally accepted best practices. 

## ApexUnitTestClassShouldHaveAsserts

Apex unit tests should include at least one assertion. This makes the tests more robust, and using assert with messages provide the developer a clearer idea of what the test does.

This rule is defined by the following Java class:

[net.sourceforge.pmd.lang.apex.rule.bestpractices.ApexUnitTestClassShouldHaveAssertsRule](https://github.com/pmd/pmd/blob/master/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestClassShouldHaveAssertsRule.java)

### Example

@isTest public class Foo { public static testMethod void testSomething() { Account a = null; // This is better than having a NullPointerException // System.assertNotEquals(a, null, 'account not found'); a.toString(); } }

### Properties

| Name | Default Value | Description |
| --- | --- | --- |
| cc_categories | [Style] | Code Climate Categories |
| cc_remediation_points_multiplier | 1 | Code Climate Remediation Points multiplier |
| cc_block_highlighting | false | Code Climate Block Highlighting |

## ApexUnitTestShouldNotUseSeeAllDataTrue

Apex unit tests should not use @isTest(seeAllData=true) because it opens up the existing database data for unexpected modification by tests.

This rule is defined by the following Java class:

[net.sourceforge.pmd.lang.apex.rule.bestpractices.ApexUnitTestShouldNotUseSeeAllDataTrueRule](https://github.com/pmd/pmd/blob/master/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestShouldNotUseSeeAllDataTrueRule.java)

### Example

@isTest(seeAllData = true) public class Foo { public static testMethod void testSomething() { Account a = null; // This is better than having a NullPointerException // System.assertNotEquals(a, null, 'account not found'); a.toString(); } }

### Properties

| Name | Default Value | Description |
| --- | --- | --- |
| cc_categories | [Style] | Code Climate Categories |
| cc_remediation_points_multiplier | 1 | Code Climate Remediation Points multiplier |
| cc_block_highlighting | false | Code Climate Block Highlighting |

## AvoidGlobalModifier

Global classes should be avoided (especially in managed packages) as they can never be deleted or changed in signature. Always check twice if something needs to be global. Many interfaces (e.g. Batch) required global modifiers in the past but don’t require this anymore. Don’t lock yourself in.

This rule is defined by the following Java class: 

[net.sourceforge.pmd.lang.apex.rule.bestpractices.AvoidGlobalModifierRule](https://github.com/pmd/pmd/blob/master/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/AvoidGlobalModifierRule.java)

### Example

global class Unchangeable { global UndeletableType unchangable(UndeletableType param) { // ... } }

### Properties

| Name | Default Value | Description |
| --- | --- | --- |
| cc_categories | [Style] | Code Climate Categories |
| cc_remediation_points_multiplier | 1 | Code Climate Remediation Points multiplier |
| cc_block_highlighting | false | Code Climate Block Highlighting |

## AvoidLogicInTrigger

As triggers do not allow methods like regular classes they are less flexible and suited to apply good encapsulation style. Therefore delegate the triggers work to a regular class (often called Trigger handler class).

This rule is defined by the following Java class:

[net.sourceforge.pmd.lang.apex.rule.bestpractices.AvoidLogicInTriggerRule](https://github.com/pmd/pmd/blob/master/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/AvoidLogicInTriggerRule.java)

### Example

trigger Accounts on Account (before insert, before update, before delete, after insert, after update, after delete, after undelete) { for(Account acc : Trigger.new) { if(Trigger.isInsert) { // ... } // ... if(Trigger.isDelete) { // ... } } }

### Properties

| Name | Default Value | Description |
| --- | --- | --- |
| cc_categories | [Style] | Code Climate Categories |
| cc_remediation_points_multiplier | 1 | Code Climate Remediation Points multiplier |
| cc_block_highlighting | false | Code Climate Block Highlighting |
