Prefer Composition over Inheritance
WarningClean ABAP & Modern Syntax#EC PREF_COMPOSITIONRemote Enabled
Overview
Prefer composition over inheritance.
ATC Finding Message:
Prefer composition over inheritance; only CX_* classes should inherit
Rationale
Inheritance creates tight coupling between subclasses and parent implementations, exposes internal state, and complicates refactoring. With the exception of exception hierarchies (CX_* classes) which require inheritance to participate in ABAP error handling, business objects should favor composition by holding references to collaborating classes and delegating tasks.
Non-Compliant
abap
CLASS lcl_order_processor DEFINITION INHERITING FROM lcl_order_validator.
ENDCLASS.Compliant
abap
CLASS lcl_order_processor DEFINITION.
PUBLIC SECTION.
METHODS constructor IMPORTING io_validator TYPE REF TO lif_order_validator.
ENDCLASS.Configuration & Thresholds
This check can be fine-tuned directly in the ATC Check Variant Editor (CHKV) in Eclipse ADT:
| Parameter Name | Data Type | Default Value | Description & Guidance |
|---|---|---|---|
ALLOW_TEST_CLASSES | Boolean | false | Allow class inheritance inside ABAP Unit test includes |
Suppression
To suppress findings for this rule, append the pseudo-comment to the statement:
abap
" Standard pseudo-comment:
... "#EC PREF_COMPOSITION
" SAP CI_ prefix format:
... "#EC CI_PREF_COMPOSITIONRelated Rules
- Limit Class Inheritance Depth (Favor Composition over Inheritance) —
#EC MAX_INHERIT - Design for Inheritance or Prohibit It (FINAL) —
#EC PREF_FINAL_CLASS - Prefer NEW #( ) over CREATE OBJECT —
#EC PREF_NEW_OPERATOR
Technical Details
- Native ATC Class:
ZCL_SKYLINT_PREF_COMPOSITION - Implementation File: zcl_skylint_pref_composition.clas.abap