diff --git a/rules/S8126/apex/metadata.json b/rules/S8126/apex/metadata.json new file mode 100644 index 00000000000..2aa687f98ad --- /dev/null +++ b/rules/S8126/apex/metadata.json @@ -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" + } +} diff --git a/rules/S8126/apex/rule.adoc b/rules/S8126/apex/rule.adoc new file mode 100644 index 00000000000..23c945a19fe --- /dev/null +++ b/rules/S8126/apex/rule.adoc @@ -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 + + * S2166 diff --git a/rules/S8126/metadata.json b/rules/S8126/metadata.json new file mode 100644 index 00000000000..2c63c085104 --- /dev/null +++ b/rules/S8126/metadata.json @@ -0,0 +1,2 @@ +{ +}