Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

ABAP Modularization

ABAP Modularization

Modularization in ABAP (Advanced Business Application Programming) involves the practice of breaking down large and complex programs into smaller, more manageable units. This approach improves code readability, maintainability, and reusability. Several techniques and constructs are available in ABAP to support modularization:

1. Function Modules:

Function modules are reusable code units that encapsulate a specific set of related functions. They allow you to modularize your ABAP programs by providing a way to call the same functionality from different parts of your application.

Example of defining a function module:

      FUNCTION Z_CALCULATE_TAX.
  DATA: lv_tax TYPE p DECIMALS 2.
  " Calculate tax logic here.
  lv_tax = ...  " Tax calculation logic.
  " Return the calculated tax.
  EXPORT lv_tax TO MEMORY ID 'TAX'.
ENDFUNCTION.

 

Calling a function module:

DATA lv_tax TYPE p DECIMALS 2.
CALL FUNCTION 'Z_CALCULATE_TAX' RECEIVING rv_tax = lv_tax.
     

 

2. Include Programs:

Include programs are reusable code snippets that can be included in other ABAP programs. They are particularly useful for storing common routines, data declarations, or utility functions that are used across multiple programs.

Example of an include program:

DATA: lv_employee_id TYPE i,
      lv_employee_name TYPE string.
   

 

Including an include program in another program:

INCLUDE Z_INCLUDE_PROGRAM.
   

 

3. Subroutines (FORMs):

Subroutines, defined using the FORM keyword, allow you to encapsulate a specific task within a program. They are useful for modularizing logic within a program and are typically used for actions that do not need to return values.

Example of defining a subroutine:

FORM calculate_discount CHANGING cv_discount TYPE p DECIMALS 2.
  " Calculate discount logic here.
  cv_discount = ... " Discount calculation logic.
ENDFORM.
     

 

Calling a subroutine:

     DATA lv_total_amount TYPE p DECIMALS 2.
DATA lv_discount TYPE p DECIMALS 2.

PERFORM calculate_discount USING lv_total_amount
                           CHANGING lv_discount.

 

4. Classes and Methods:

Object-oriented programming (OOP) in ABAP allows you to define classes and methods. Classes encapsulate data (attributes) and behavior (methods) into objects. Methods are functions associated with classes that perform specific tasks.

Example of defining a class and method:

CLASS Z_EMPLOYEE DEFINITION.
  PUBLIC SECTION.
    DATA: employee_id TYPE i,
          employee_name TYPE string.
    METHODS: constructor,
             get_employee_info IMPORTING i_employee_id TYPE i,
             display_employee_info.
ENDCLASS.

 

Using a class and its methods:

DATA lo_employee TYPE REF TO z_employee.
CREATE OBJECT lo_employee.
lo_employee->get_employee_info(123).
lo_employee->display_employee_info( ).

 

5. Macros:

Macros are used to define reusable code snippets that can be expanded at compile time. They are particularly useful for generating repetitive code.

Example of defining a macro:

DEFINE discount.
  " Discount calculation logic here.
  result = ... " Discount calculation logic.
END-OF-DEFINITION.
   

 

Using a macro:

discount.
   

 

6. Methods in Function Modules:

Function modules can also contain methods, allowing you to modularize functionality within a function module.

Example of defining a method within a function module:

    FUNCTION Z_CALCULATE_TAX.
  CLASS-METHODS: calculate_tax IMPORTING i_amount TYPE p DECIMALS 2
                                     EXPORTING e_tax TYPE p DECIMALS 2.
ENDFUNCTION.

 

Using a method within a function module:

DATA lv_tax TYPE p DECIMALS 2.
CALL METHOD (mf_function_module)=>calculate_tax
  EXPORTING i_amount = lv_amount
  IMPORTING e_tax = lv_tax.