Module 7: Modularization Techniques
Modularization means breaking large programs into smaller, reusable, and maintainable units.
Good modularization improves readability, testing, reuse, and performance.
1. Why Modularization?
Without modularization:
- Programs become large and unreadable
- Debugging becomes difficult
- Code reuse is poor
- Changes introduce bugs easily
Modern ABAP strongly prefers object-oriented modularization (classes & methods). Procedural modularization still exists mainly for legacy code.
2. Includes
What is an Include?
An include is a reusable source code block inserted into a program at compile time.
INCLUDE zmy_include.
Typical Uses
-
Declarations
-
Constants
-
Type definitions
-
Common logic (legacy)
Using includes for business logic is considered obsolete in modern ABAP.
Use classes and interfaces instead of includes for logic reuse.
3. Subroutines (FORM routines)
What is a Subroutine?
A FORM is a procedural modularization unit used in classic ABAP reports.
FORM calculate_total USING iv_qty TYPE i
iv_price TYPE p
CHANGING cv_total TYPE p.
cv_total = iv_qty * iv_price.
ENDFORM.
Call:
PERFORM calculate_total
USING lv_qty lv_price
CHANGING lv_total.
Characteristics
-
Procedural
-
No exception handling
-
No encapsulation
-
Global data access possible
FORM routines should not be used in new developments. They exist mainly for maintenance of old reports.
4. Function Modules
What is a Function Module?
A Function Module (FM) is a reusable procedure stored in a Function Group.
CALL FUNCTION 'Z_CALCULATE_TOTAL'
EXPORTING
iv_qty = lv_qty
iv_price = lv_price
IMPORTING
ev_total = lv_total.
Key Features
-
Can be remote-enabled (RFC)
-
Supports exceptions
-
Widely used in classic SAP
-
Central to BAPIs
Function Modules are still heavily used for:
-
RFCs
-
BAPIs
-
Integration scenarios
Limitations of Function Modules
-
Procedural, not OO
-
Global data via function groups
-
Harder to unit test
-
Poor encapsulation
For new developments:
-
Use classes and methods
-
Wrap existing FMs inside OO layers if required
5. Modern Modularization: Classes & Methods (Recommended)
Example: Method-Based Modularization
CLASS zcl_calculator DEFINITION.
PUBLIC SECTION.
METHODS calculate_total
IMPORTING iv_qty TYPE i
iv_price TYPE p
RETURNING VALUE(rv_total) TYPE p.
ENDCLASS.
CLASS zcl_calculator IMPLEMENTATION.
METHOD calculate_total.
rv_total = iv_qty * iv_price.
ENDMETHOD.
ENDCLASS.
Usage:
DATA(lv_total) = NEW zcl_calculator( )->calculate_total(
iv_qty = 10
iv_price = 50 ).
-
Encapsulation
-
Testability
-
Reusability
-
Clean ABAP compliant
6. Global vs Local Data
6.1 Global Data
Defined outside methods or forms.
DATA gv_total TYPE p.
Problems with Global Data
-
Side effects
-
Hard to debug
-
Not thread-safe
-
Poor testability
Global data is a major source of bugs in large ABAP systems.
6.2 Local Data (Preferred)
Defined inside methods, forms, or blocks.
METHOD calculate.
DATA(lv_sum) = 10 + 20.
ENDMETHOD.
Always prefer local data + parameters over global variables.
7. Comparison of Modularization Techniques
| Technique | Status | Usage Recommendation |
|---|---|---|
| INCLUDE | Legacy | Only for declarations |
| FORM | Legacy | Avoid in new code |
| Function Module | Semi-Modern | Use for RFC / BAPI |
| Class & Method | Modern | Preferred approach |
8. Common Beginner Mistakes
-
Writing logic directly in reports
-
Overusing global data
-
Creating new FORM routines
-
Calling function modules instead of methods
-
Mixing procedural and OO styles randomly
- Summary
-
Modularization is essential for maintainable ABAP
-
Includes and FORM routines are legacy
-
Function Modules are still relevant for integration
-
Classes and methods are the modern standard
-
Local data improves reliability and testability
10. Practice Exercises
-
Convert a FORM routine into a method.
-
Wrap a function module call inside a class.
-
Refactor global variables into method parameters.
-
Identify legacy modularization in an existing program.
11. What’s Next?
➡️ Module 8: Selection Screens & Reports
Interviewers love candidates who can identify legacy code and modernize it.