Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions rules/S8126/apex/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"title": "Custom exception classes should follow proper naming conventions",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant/Issue",
"constantCost": "5 min"
},
"tags": [
"convention",
"naming"
],
"defaultSeverity": "Minor",
"ruleSpecification": "RSPEC-8126",
"sqKey": "S8126",
"scope": "Main",
"defaultQualityProfiles": [
"Sonar way"
],
"quickfix": "unknown",
"code": {
"impacts": {
"MAINTAINABILITY": "LOW"
},
"attribute": "CONVENTIONAL"
}
}
67 changes: 67 additions & 0 deletions rules/S8126/apex/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
This rule raises an issue when a custom exception class does not end with the suffix "Exception".

== Why is this an issue?

Custom exception classes serve as a way to handle specific error conditions in your Apex code. When these classes don't follow proper naming conventions, it creates several problems.

First, unclear naming makes code harder to understand and maintain. A class named `MyError` or `CustomHandler` doesn't immediately signal to other developers that it's an exception class. This lack of clarity can lead to confusion when reading or debugging code.

Second, Apex follows Java conventions where exception classes should end with "Exception". This convention is widely recognized and expected by developers. Breaking this convention makes your code inconsistent with established patterns.

Third, proper exception naming helps with code organization and searchability. When all exception classes follow the same naming pattern, developers can quickly identify and locate them in the codebase.

Finally, some development tools and static analysis systems rely on naming conventions to provide better support, such as syntax highlighting, code completion, and error detection.

=== What is the potential impact?

Poor naming conventions for exception classes can lead to:

* Reduced code readability and maintainability
* Confusion for team members and future developers
* Inconsistency with Apex and Java conventions
* Difficulty in identifying exception classes during code reviews
* Potential issues with development tools that rely on naming patterns

== How to fix it

Rename your custom exception class to end with 'Exception' and ensure it properly extends the Exception class. Choose a descriptive name that clearly indicates the type of error condition it represents.

=== Code examples

==== Noncompliant code example

[source,apex,diff-id=1,diff-type=noncompliant]
----
public class MyError extends Exception {} // Noncompliant

public class CustomHandler extends Exception {} // Noncompliant

public class ValidationIssue extends Exception {} // Noncompliant
----

==== Compliant solution

[source,apex,diff-id=1,diff-type=compliant]
----
public class MyCustomException extends Exception {}

public class CustomProcessException extends Exception {}

public class ValidationException extends Exception {}
----

== Resources

=== Documentation

* Apex Exception Class - https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_exception_methods.htm[Official Salesforce documentation on the Exception class and its methods]

* Exception Handling in Apex - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_exception_definition.htm[Comprehensive guide on exception handling and custom exceptions in Apex]

=== Standards

* Java Naming Conventions - https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html[Oracle's official Java naming conventions, which Apex follows for exception classes]

=== Related rules

* RSPEC-2166 - https://rules.sonarsource.com/java/RSPEC-2166/[Classes named like "Exception" should extend "Exception" or a subclass (Java)]
2 changes: 2 additions & 0 deletions rules/S8126/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}