Saturday, January 18, 2014

TAW10 - MODULARIZATION

OPTIONS FOR USING MODULARIZATION UNITS


modularization unit is a part of a program, which encapsulates a particular function.
Storing a part of the source code in a module
  improves the transparency of the program, as the program is now more function-oriented;

  enables the encapsulated function in the program to be reusedseveral times; and

  saves the trouble of reimplementing the entire source code on each occasion.

Modularization also makes it easier to maintain programs, as you do not need to make changes to the function at various points in the main program, but only in the particular modularization units.


LOCAL PROGRAM MODULARIZATION


You can carry out local program modularization in two ways:
  Performing subroutines, also known as form routines

  Calling methods in local classes
These modularization units are only available in the program in which they are implemented and no other program must be loaded to the user context at runtime. Local classes, methods, and subroutines can have the same name in different programs. This will not result in any conflicts, as the source code for the programs is handled separately in the main memory of the application server.

GLOBAL MODULARIZATION


The two techniques of global modularization in the ABAP programming language involve the use of
  function modules that are organized in function groups; and

  methods in global classes.
A function group is a collection of function modules, which have similar functions and process the same data.
Any number of programs can use these globally defined modularization units at the same time. The units are stored centrally in the Repository and loaded when called into the context of the calling program.

SEPARATING DATA


The modularization units that are called should not use the data objects of the calling program directly.
Conversely, the data in the modularization units should also not be changed directly by the calling program.
This principle is known as data encapsulation.
Data encapsulation is an important aid in developing a transparent and maintainable source code.

DATA TRANSPORT BETWEEN  THE PROGRAM AND THE MODULARIZATION UNIT

Parameters are used to exchange data between the program and the module.
The total number of parameters in a modularization unit is termedinterface or signature.
Parameters can be of three types:
Importing parameters are used to pass data to the modularization unit. Exporting parameters return data from the modularization unit to the caller. Changing parameters are used not only to pass data to the modularization unit, but also to return the data after it is changed.


SIMPLE EXAMPLE OF A SUBROUTINE


A subroutine is a modularization unit within a program. It normally uses values from data objects and returns values too. A subroutine is introduced within a FORM and an ENDFORM statement. The PERFORM statement is used for calling a subroutine.


PASSING PARAMETERS - VISIBILITY OF GLOBAL VARIABLES

The variables defined in the main program are visible globally within the program and can be changed at any point during the entire program. This means that subroutines that are defined within the main program can change them too.

PASSING PARAMETERS - DEFINING AN INTERFACE

Placeholders – called formal parameters – are used to call a subroutine with different data objects for each situation.
When the subroutine is called, the required global variables replace these placeholders, which together form the subroutine interface.
When the subroutine is called, its processing must be referenced to real variables. Therefore, the formal parameters must be specialized by means of the corresponding global variables, which are called actual parameters. This assignment of actual parameters to formal parameters when calling a subroutine is called parameter passing.

WAYS OF PASSING INTERFACE PARAMETERS


The way in which main program variables are passed to the formal parameters of the subroutine is called the pass type.
There are three pass types for subroutines:
Call by value is the pass type you use to make the value of a global variable available to the subroutine in the form of a variable copy.
However, creating copies especially for large internal tables can be time-consuming.
Call by value and result is the pass type you use to transfer the value of a global variable to the subroutine. You then write the result or the fully processed final value of the copy back to the original. However, it can be time-consuming to create copies and write back values especially for large internal tables.
Call by reference is the pass type you use if you want to run subroutine processing directly on the specified actual parameter. It is a useful way of avoiding the time-consuming creation of copies for large internal tables.

DEFINING AND CALLING SUBROUTINES


A subroutine is structured within a FORM and an ENDFORM statement.
You specify the name and the interface of the subroutine after FORM, after which the statements of the subroutine follow.
In the interface definition, you list the formal parameters of the subroutine, such as f1f2, and f3 and the required pass type is specified for each parameter.
For call by value, list each of the formal parameters with the VALUE prefixunder USING.
For call by value and result, list each of the formal parameters with the VALUE prefix under CHANGING.
For call by reference, list each of the formal parameters without the VALUE prefix under CHANGING.
The order of specification determines the assignment of the actual parameters to the formal parameters. In the graphic, a is passed to f1b to f2; and c to f3.


TYPING THE INTERFACE PARAMETERS


Generic typing of a parameter occurs when a formal parameter is either typed using TYPE ANY or not typed at all.
Actual parameters of any type can be transferred to such a parameter.
At runtime, the type of the actual parameter is determined and assigned to the formal parameter when the subroutine is called. This is called type inheritance. However, there is danger of atype conflict, and a runtime error may occur.
Concrete or exact typing of a formal parameter involves specifying a global or built-in type in the TYPE addition. You use this typing method to determine that only actual parameters of the specified type can be passed to the subroutine.


TYPING THE INTERFACE PARAMETER FOR STRUCTURE AND INTERNAL TABLES

Formal parameters for data objects, such as structures and internal tables,must always be typed. This ensures that their components can be accessed within the subroutine.

VISIBILITY OF GLOBAL AND LOCAL DATA OBJECTS


Global data objects
  are the variables defined in the main program called; and

  are visible in the entire main program and in every subroutine called;
In contrast, local data objects are the local variables defined within a subroutine: they exist only in that subroutine, just like the formal parameters.


SYNTAX EXAMPLE: LOCAL AUXILARY VARIABLES FOR ROUNDING


In the syntax example, you should round off the result of the percentage calculation internally to the nearest whole number with one decimal place. However, it should be returned with two decimal places.

IMPLEMENTING A SUBROUTINE CALL


You must generate the PERFORM-statement for calling a subroutine into your source code.
You define the subroutine and then save your main program.
The newly defined subroutine appears in the navigation area.
You can now move it to the required call point in your program by drag and drop.
In the final step, replace the formal parameters in the generated source code with corresponding actual parameters.


SUBROUTINE IN DEBUGGING MODE



You can execute the entire subroutine without stopping by choosing Execute.
Alternatively, you can use Single Step to stop at the first statement of the subroutine and trace its operations in more detail.
You can execute the rest of the subroutine (if the current statement is located in a subroutine) without stopping by choosing Return. Processing only stops after the subroutine is complete.


DEBUGGING - CALL STACK


Under the Standard tab page in the Debugger you can see from which programs the subroutine was called. The tool for this is theCall Stack.

FUNCTION GROUP


function module is a subroutine, which has a corresponding function stored centrally in the Function Library of the SAP system. It has an interface with parameters for importing or exporting data.
function group is a collection of function modules that have similar functions or process the same data.
A function group can contain the same components as an executable program:
Data objects, for example, are global in relation to the function group and can be changed by all function modules within the group. Subroutines and Screens are also examples of components, which can also be called from all function modules in the group.


FUNCTION MODULE


The main advantage of function modules is their reusability.
Here we examine the components of a function module:
The properties of a function module include a short description and the function group it belongs to.
Like subroutines, a function module contains its own local type and data object definitions within itself.
The third component – interface – of a function module itself comprises more components or elements:
  Import parameters, which are optional, need not be supplied with data during the call. Values or variables of the calling program can be transferred to these when the function module is called.

  Export parameters are also always optional. The calling program accepts the output of the function module by assigning a “receiving variable” to an export parameter.

  Changing parameters enable you to transfer the variables of the calling program, which were changed by the function module, to these parameters.

  Exceptions are raised by the function module when errors occur. They provide information on the relevant processing error in the function module. Exceptions should be handled by the calling program.


DATA OBJECTS WITHIN A FUNCTION GROUP


If a program calls a function module, the entire function group containing that module is loaded. This function group remains loaded in the working memory until the calling program is closed.
Consequently, if another function module of this group is called, it is processed at once. There is no need to reload this module, as the same global data can be accessed from the function group. Thus if a function module that writes values to the global data of the function group is called, then the other function modules in the group can also access this data when they are called in the same program run.


SEARCHING FOR FUNCTION MODULES


You can search for function modules in many ways:
  The application-related search through the application hierarchy helps you search for function modules within one or more application components.

  The application-independent search or free search through the Repository Information System helps you locate function modules that are independent of application components.

  The program-related search enables you to search for a function module that is called within an existing program.


FUNCTION MODULE INTERFACE


The interface of the function module comprises the import, export, and changing parameters, and the exceptions. It is mandatory for the nonoptional parameters to be supplied with data when the function module is called.

DOCUMENTATION AND TEST ENVIRONMENT


Function modules that are intended and released for use in other programs should be documented.
Function module documentation describes the function of the module. Parameter documentation tells you how individual parameters or exceptions are used.
You can access the function module documentation by choosingFModule Documentation.
You can test the output of the function module in the test environment prior to installing it in your program. An input template allows you to specify values for the import and changing parameters. If the function module triggers an exception due to an error situation, it is displayed with its message text.

CALLING A FUNCTION MODULE


Function modules are called using the CALL FUNCTION statement.
In the EXPORTING block, the system passes values to theimport parameters of the function module.
In the IMPORTING block of the call, actual parameters are assigned to the export parameters and can be used to access the results of the call.
In the call syntax you always specify the name of the interface parameter (formal parameter) on the left side of the parameter assignment and the data object or the value of the calling program (actual parameter) on the right side.


CREATING SOURCE CODE FOR A FUNCTION MODULE CALL


You can choose Pattern to generate a function module call in the source code. Alternatively, you can display the function group in the navigation area and drag and drop into the editor area to generate the call in the source code. You must then fill in the actual parameters and, if necessary, implement exception handling.



HANDLING EXCEPTIONS


The function module raises the corresponding exceptions in case of any application error. This causes the processing of the function module to be canceled and the system returns to the calling program.
If the exception is listed in the EXCEPTIONS block of the call, the specified return code is entered in the sy-subrc system field of the calling program.


CREATING A FUNCTION GROUP


Some steps are involved in the creation of a function group:
  Select the object list for a function group in the Object Navigator.

  Enter the name of the new function group; choose ENTER orDisplay; and confirm the creation of the new function group in the resulting dialog box.

  Next, a dialog box with the attributes of the function group appears.

  Enter a short text and choose Save.

  Assign the function group to a package.

CREATING A FUNCTION MODULE
Creating a function module involves several steps:
  Choose the function group where you want to create the new function module.

  Display the object list for the function group in the navigation area of the Object Navigator.

  In the hierarchical tree structure, open the context menu for the function group and choose Create → Function Module.

  In the resulting Create Function Module dialog box, enter a name and a short text for the function module.

  Click Save.

  Assign the function module to a package.

EDITING THE SOURCE CODE

You can switch to the Source Code tab page in order to implement the functions of the function module after defining the corresponding IMPORTING and EXPORTING parameters.

BUSSINESS OBJECTS AND BAPIs


The Business Object Repository (BOR) in the SAP system contains business objects.
business object is a class and corresponds to an SAP table or a table hierarchy. It has BAPIs (Business Application Programming Interfaces) as methods to access the data in the SAP system.
BAPIs are used for basic functions of a business object, such as
  creating an object;

  retrieving the attributes of an object;

  changing the attributes of an object; and

  listing the objects.

USING BAPIs

The functions of a BAPI are encapsulated in a function module that can be called remotely by ABAP programs of the same SAP system, as well as by external programs.


STANDARD BAPIs


You can access standard methods in the form of BAPIs with standardized names:
GetList returns a list of available objects that meet the specified selection criteria.
GetDetail returns the detailed information or attributes for an object. Note that you must specify the complete key.
Create, Change, Delete, Cancel is a BAPI that allows you to create, change, and delete objects.
AddItem, RemoveItem is another example of a BAPI. This helps you to add and remove subobjects.


BAPIs EXPLORER


You can use the BAPI Explorer to list business objects and their BAPIs with reference to the application.
You can call the BAPI Explorer using the path:
SAP Easy Access menu → Tools → Business Framework→ BAPI Explorer.
You can display the relevant details of the required business object or BAPI in the work area by selecting the BAPI. You can then navigate to the Function Builder by double-clicking the displayed function module.

PROPERTIES OF A BAPI FUNCTION MODULE
BAPI function modules have certain technical requirements:
  The naming convention should beBAPI_<business_object_name>_<method_name>.

  It should be remote enabled.

  It cannot contain any user dialogs or messages.

  The interface parameters are typed with components of structures from the ABAP Dictionary that were created for this BAPI. The name prefix for such structures should be BAPI_.

  It must not contain any changing parameters until release 4.6.

  The special export parameter RETURN helps report the errors to the user.

BAPI CALL IN AN ABAP PROGRAM


In order to use a BAPI within the same SAP system, you call the relevant function module directly.
Conversion or reconversion is done by using the function modules from the BACV function group (package SBF_BAPI).


REPRESENTATION OF A GLOBAL CLASS


The object-oriented enhancements to ABAP include globalclasses that make functions available in the form of methods.
The methods have an interface, which is known as a signature. The signature comprises import, export, and changing parameters and exceptions.
Classes also have other components, such as global data objects known as attributes. All the methods of a class can access the attributes of that class.

EXAMPLE OF THE ACCESS OPTIONS FOR A GLOBAL CLASS


Attributes are normally encapsulated in the class and can only be read or changed by using the methods of the same class. In contrast to function modules, classes allow you to make specific attributes visible to the users of the class by defining public andprivate attributes. This distinction can also be applied to methods. The public methods are available outside the class and the private methods can only be called by other methods of the same class.


CLASSSES AND OBJECTS


We can pinpoint the major differences between global classes and function groups:
  A function group with its global data objects can only be loaded once to the program context.

  A global class can be loaded many times. This describes themultiple instantiation of the class.

  The values in the global data objects of a function group are the same for all function module calls.

  Because of multiple instantiation, a global class can have several instances known as objects, as well as different attribute values. Consequently, a method can access different values in the attributes, depending on the instance for which it was called.


DEFINITION OF ATTRIBUTES


Attributes that can have a different value for each instance are known as instance attributes.
In contrast, static attributes exist only once for each program context, regardless of how many class instances are generated.
You can choose the Attributes tab to open the list of the attribute definitions in the class.

DEFINITION OF METHODS

A static method can be called directly without generating an instance of the class. In contrast, with the instance method, you first generate an instance and then call the method specifically for this instance. You can choose the Methods tab to open the list of all method definitions in the class.

METHOD PARAMETERS


The parameters for a method can be accessed from the method list by placing the cursor on the required method and selecting the Parameter option. Unlike in function modules, there is no separate list for export, import, and changing parameters. Hence, the parameter type is entered in the Type column instead.


METHOD EXCEPTIONS


You can display the exceptions for a method by placing the cursor on the required method and selecting the Exceptionsoption. Class-based exceptions were introduced with Basis release 6.10. A method can either raise new class-based exceptions or classic exceptions, but not both together. The new exception concept requires that you specify the names of the exception classes in it; whereas, the classic exception concept allows you to choose any exception identifier.

THE CLASS BUILDER TESTING ENVIRONMENT


Methods can be tested by selecting the Execute Method option. You need not generate an instance to test a static method – you can execute a static method immediately. After you choose theExecute option, the importing parameters if any, appear on the screen. You should supply the parameters with values. On testing the method, the result of the exporting parameter will be displayed.


METHOD CALLS USING DRAG AND DROP


The CALL METHOD statement is used to call methods. You can select a method name in the navigation area and drag it to the editing area. For static methods, you then specify the name of the class and method, separated by the so-called static component selector the “=>”. The parameters are then passed in an EXPORTING block and an IMPORTING block. You can, alternatively, select the Pattern option to find the Call Methodoption under ABAP Objects Pattern.

HANDLING EXCEPTIONS: CLASSIC EXCEPTIONS


A return code is assigned to the exception in the EXCEPTIONS block of the method call. This return code is placed in the system field sy-subrc when the method terminates with this classic exception. The calling program can then react to the exception by querying sy-subrc.


HANDLING EXCEPTIONS: CLASS BASED EXCEPTIONS

In class-based exceptions, the call must be bound between the TRY and ENDTRY statements. The processing block that begins with the CATCH <exception class> statement handles the actual exception. The exception class that is to be handled is denoted by <exception class> The processing is terminated and the system branches directly to the relevant CATCH block if the corresponding exception is raised somewhere within the TRY–ENDTRY block.


HANDLING EXCEPTIONS WITH EXCEPTION CLASSES

The statements for handling the class-based exceptions are generated when a method is called by drag and drop or by usingPattern, You can then remove the comments and implement the CATCH blocks.


CREATING OBJECTS AND CALLING METHODS

Reference variables are defined to generate and address instances of classes. They act as pointers that can be directed to corresponding instances. They are defined by the statement,DATA gr_ref_name TYPE REF TO class_name.
Once the reference variable is used to create an instance, it no longer holds the initial value and points to that instance.
An instance of the class specified in the definition of the reference variables can be generated by the statement,CREATE OBJECT gr_ref_name.
You can call the methods of an instance using the statement,CALL METHOD gr_ref_name->method_name.


CREATING GLOBAL CLASSES IN THE OBJECT NAVIGATOR
To create a global class, you can open the context menu for your package in the navigation area and choose Create → Class Library → Class.
You then enter a short description and several other properties, along with the name of the class in the resulting dialog box.
You can also choose Class/Interface from the dropdown list in the navigation area, to enter the name of the new class. When you click Display, you confirm that you want to create this new class.

CALLING THE SOURCE CODE EDITOR


You can implement the source code for a method by selecting the method in the method list and choosing the Source Codeoption. It is usually written between the METHOD <method_name> and the ENDMETHOD statements.


SHOWING THE SIGNATURE IN THE SOURCE CODE EDITOR


The signature for the method can be displayed while editing the source code by selecting the Signature option.


DEFINING A LOCAL CLASS


A class consists of the definition and the implementationblocks. The definition block comprises the attributes and the signatures of the methods; whereas, the implementation block contains the source code of the methods. The CLASS ...ENDCLASS statement defines local object types. The visibility sections, such as PUBLIC SECTION and PRIVATE SECTION, are mentioned in the definition block.


SYNTAX FOR STATIC METHODS

When methods are called by a program, they can receive values from it – as well as pass the values back to it – using asignature. There can be any number of IMPORTING, EXPORTING, and CHANGING parameters. All parameters can be passed as values or as references.
You can define a return value using the RETURNING parameter; whereas, you cannot define any EXPORTING and CHANGING parameter.


IMPLEMENTING AND USING STATIC METHOD

You can define a local class lcl_compute with a static methodget_percentage in the definition block. The method is then implemented in the implementation block. The static method call from the main program is identical to a static method call in a global class.

No comments:

Post a Comment