Modularization in SAP ABAP

FREE Online Courses: Knowledge Awaits – Click for Free Access!

In this tutorial, we’ll be learning about SAP ABAP modularization techniques. They make programs readable, flexible, and reusable. Let’s dive right into the world of modules!

What is Modularization in SAP ABAP?

  • Modularization essentially means – breaking things down into simpler forms.
  • This involves creating small chunks of code called ‘modules’ that perform a specific task.
  • A module simply contains a sequence of ABAP statements that the program executes in a logical manner.
  • Creating and using modules in programming creates easy readability and enhances understanding of code.
  • This also promotes reusability – that is, once you write a module to perform a specific task, you can reuse the same module when having to perform the same task again.
  • Breaking down tasks into logical blocks is a simple and easy way to make your code readable, and promote the good coding practice.

Need & Advantages of SAP ABAP Modularization

  • The modules, or processing blocks, enhance how we structure the program.
  • They make it easy for us to read the code.
  • They also make it easy to maintain the code.
  • These help to avoid redundancy.
  • They introduce ‘reusability’ features in code – i.e. we can reuse a block defined in one area into another area as well.
  • Since it divides the problem into smaller portions, it is easy to debug.

Various Modularization Techniques in SAP ABAP

  1. Macros
  2. Include Files
  3. Subroutines
  4. Function Modules
  5. Methods & Classes

Modularization can be of two broad categories –

  1. Modules through source code
      1. Local Macros
      2. Glocal Include
  2. Modules at ABAP programs (processing blocks)
      1. Subroutines
      2. Function modules

SAP- ABAP Macro

  • We can use macros when we want to reuse specific statements.
  • If we have a long calculation or a complex print statement that we do not want to keep typing again and again but still want to include, we can put it in a macro.
  • The scope of a macro is only within the program in which we declare it i.e. we cannot use it outside that program.
  • We can define macros using the DEFINE statement and close with END-OF-DEFINITION statement.
  • A macro basically acts as a shorter placeholder for longer statements.

SYNTAX

DEFINE <macro_name>.
        ...
        <STATEMENTS>
        ...
        END-OF-DEFINITION.

EXAMPLE

DEFINE ZDATAFLAIR_MACRO.
        
        WRITE: “This is the first macro.”.
        WRITE: “This is the second macro.”.
        WRITE: “This is the third macro.”.
        WRITE: “This is the fourth macro.”.
        WRITE: “This is the fifth macro.”.

        END-OF-DEFINITION.

OUTPUT

This is the first macro.
This is the second macro.
This is the third macro.
This is the fourth macro.
This is the fifth macro.

Include Programs

  • Include programs helps us use our entire code into another program.
  • This way, we can create small chunks of programs stored separately and then use them via include in one complex program.
  • Thus we make the final program easy to read.
  • Also, we can use the program multiple times so this also promotes reusability.
  • This is the same as copying the entire program into another, or nesting it within one.
  • We can use the INCLUDE statement to use this modularization technique.

SYNTAX

INCLUDE <program_name>

EXAMPLE

ZS_DATAFLAIR_001:

REPORT ZS_DATAFLAIR_001.
INCLUDE ZDATAFLAIR.
WRITE: “Hello, this is the parent program.”.

ZDATAFLAIR:

WRITE: “Hello, this is the INCLUDE program.”.

OUTPUT

Hello, this is the INCLUDE program.
Hello, this is the parent program

However, there are a few rules we should keep in mind while using INCLUDE:

  1. Include programs cannot include themselves.
  2. The code within a program that we want to include must contain complete statements.

Subroutines in SAP ABAP

  • A subroutine is a module or section within the code itself that can be called multiple times.
  • It is a module, or a unit of modularization, that has a function wrapped by the source code.
  • It can be thus reused any number of times as we wish.
  • We can declare subroutines using FORM statement and end with ENDFORM.
  • We can call subroutine using PERFORM statement.
  • Subroutines can be internal or external.

SYNTAX

FORM <subroutine_name>.
…
<statements>
…
ENDFORM.

EXAMPLE

FORM ZF_DATAFLAIR_001.
WRITE: “This is a form.”.
ENDFORM.

OUTPUT

This is a form.

Types of SAP ABAP Subroutines

  1. Internal subroutines:
    • If a subroutine is in the same program which is being called, we call it an internal subroutine.
    • An internal subroutine is allowed to access data objects in the program.
  2. External subroutines:
    • If a subroutine is outside the program which is being called, we call it an external subroutine.
    • An external subroutine is not allowed to access data objects in the program and has to use the PASS option for access.

Calling an internal subroutine

SYNTAX

PERFORM <subroutine_name> [<parameters_to_pass>]

EXAMPLE

PERFORM <ZF_DATAFLAIR_001>		“Since there are no parameters in our example

OUTPUT

This is a form.
Calling an external subroutine

SYNTAX

PERFORM <subroutine_name> (<Program>)[<parameters_to_pass>]

EXAMPLE

PERFORM <ZF_DATAFLAIR_001> (ZR_SS_DATAFLAIRSAMPLE_001)

OUTPUT

This is a form.

Function Modules in SAP ABAP

  • Function modules contain statements that we can reuse.
  • However, unlike INCLUDE programs, we can run the statements within function modules on their own i.e. independently.
  • The function group puts together a couple of statements that logically belong together.
  • Hence, a function module is a type of modularisation that creates logical blocks rather than strictly processing blocks.
  • We can define function modules using FUNCTION and ENDFUNCTION statements.
  • We can use RFCs (Remote Function Calls) to call functions from outside the SAP system.

SYNTAX

FUNCTION <function_name>
…
<statements>
…
ENDFUNCTION.

EXAMPLE

FUNCTION ZF_DATAFLAIR
WRITE: “This is statement 001.”.
WRITE: “This statement can be executed independently of statement 001.”.
ENDFUNCTION.

CALLING ABOVE FUNCTION MODULE

CALL FUNCTION <module_name>
        [EXPORTING <exporting_parameters>]
        [IMPORTING <importing_parameters>]
        [CHANGING <changing_parameters>]
        [TABLES <table_names>]
        [EXCEPTIONS <exception_names>]
        [OTHERS = ro]

OUTPUT

This is statement 001.
This statement can be executed independently of statement 001.

Function Groups in SAP ABAP

  • We can contain function modules in one of many standard function groups.
  • The function modules within one function group are allowed to access global data.
  • We cannot run function groups.
  • They comprise selection screens, lists, etc.
  • When we create the container, the code will create the main and include programs on its own.
  • With respect to object-oriented programming, function groups are great for promoting data encapsulation.
  • Data encapsulation is wrapping of data and functions into one single unit.

How to Create a Function Group & Function Module

  1. Open the SAP system.
  2. Click on tcode input box and type in ‘SE80’.
  3. You will get a dropdown menu, from which you will select ‘Program’.
  4. Now we will write the name of the function group (must start with ‘Z’).
  5. Function group is created.
  6. Now that the function group is opened, you get the option to directly create a function module.
  7. Add its name and properties, include necessary files that have its source code and hit enter.
  8. Save, test and activate the created function module.

Methods & Classes in SAP ABAP

  • Methods are smaller pieces of a program created to perform a specific action.
  • Each method has a name, parameters, return type and a specific use case that it resolves.
  • A program calls a method simply in one line when it needs to perform an action.
  • A class is an encapsulation of objects, data and methods that is part of the code.
  • We call class instances as objects, which are the building blocks of object-oriented programming.
  • We have explained methods & classes in detail in our OOPS part #01 tutorial.

Summary

Thus, we learnt in this tutorial about the various techniques of modularization in ABAP. We learnt the meaning and use of modularization techniques, and the various types in ABAP. We also saw a few examples and tutorials on how to use the techniques.

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *