Method Chaining: A Modern Approach to Simplifying SAP ABAP Code

Estimated read time 10 min read

What Is Method Chaining?  

Method Chaining in ABAP allows you to call multiple methods one after the other in a single statement, without needing helper variables. This makes your code cleaner and easier to read. 

Previously, ABAP only supported chaining of attributes in class calls. But starting with ABAP 7.0 EhP2, you can also chain methods along with attributes.  

 Key Features of Method Chaining in SAP ABAP 

Chained Execution: Allows multiple methods to be executed in a single statement. Fluent Interface Design: Methods return the same object (SELF), enabling seamless chaining of operations. Enhanced Readability: Eliminates the need for intermediate variables, making the code cleaner and more intuitive. Concise Code: Reduces the number of lines of code, improving maintainability. Modern ABAP Practice: Aligns with modern programming paradigms and best practices.Ease of Debugging: Individual methods can be tested independently while supporting chaining.

Advantages of Method Chaining 

Improved Readability: Code is more concise and easier to understand. 

Fewer Variables: Reduces the need for intermediate variables.

Aligned with OOP: Encourages a clean and modern ABAP development style. 

Reusable Code: Enables building reusable and modular classes.

Fluent Interface: Helps developers create a more intuitive and fluent API for their classes. 

Example for Method Chaining: 

I have taken 3 classes and one method in each class and given implementations for all three methods of a Class. 

 

 

REPORT zch_rp_method_chaining.
CLASS lcl_discount DEFINITION.
PUBLIC SECTION.
METHODS apply_discount IMPORTING im_price TYPE i
im_discount TYPE i
RETURNING VALUE(re_discounted_price) TYPE i.
ENDCLASS.
CLASS lcl_tax DEFINITION.
PUBLIC SECTION.
METHODS apply_tax IMPORTING im_price TYPE i
im_tax_rate TYPE i
RETURNING VALUE(re_price_with_tax) TYPE i.
ENDCLASS.
CLASS lcl_final_price DEFINITION.
PUBLIC SECTION.
METHODS calculate_final_price IMPORTING im_price TYPE i
RETURNING VALUE(re_final_price) TYPE i.
ENDCLASS.

CLASS lcl_discount IMPLEMENTATION.
METHOD apply_discount.
re_discounted_price = im_price – ( im_price * im_discount / 100 ).
ENDMETHOD.
ENDCLASS.
CLASS lcl_tax IMPLEMENTATION.
METHOD apply_tax.
re_price_with_tax = im_price + ( im_price * im_tax_rate / 100 ).
ENDMETHOD.
ENDCLASS.
CLASS lcl_final_price IMPLEMENTATION.
METHOD calculate_final_price.
re_final_price = im_price.
ENDMETHOD.
ENDCLASS.

 

 

Without Method Chaining if we want to call the method :

We need to create Object reference for each class and using that reference we need to call the method.   

apply_discount Method: Accepts two parameters and returns a result.  

apply_tax Method: Takes the result of  apply_discount method as input for im_price .  

calculate_final_price : Uses the result of apply_tax as input for im_price and returns the final output

 

 

START-OF-SELECTION.
data : lo_discount type REF TO lcl_discount,
lo_tax type ref to lcl_tax,
lo_final_price type ref to lcl_final_price.

data : lv_discounted_price type i,
lv_price_with_tax type i,
lv_final_price type i.

CREATE OBJECT : lo_discount,
lo_tax,
lo_final_price.

“without method chaining
lv_discounted_price = lo_discount->apply_discount(
im_price = 500
im_discount = 10
).
lv_price_with_tax = lo_tax->apply_tax(
im_price = lv_discounted_price
im_tax_rate = 5
).
lv_final_price = lo_final_price->calculate_final_price( im_price = lv_price_with_tax ).

write :/’Final price without method chaining ‘,lv_final_price.

 

With Method Chaining 

 

“With method chaining
lv_final_price = lo_final_price->calculate_final_price(
EXPORTING im_price = lo_tax->apply_tax(
EXPORTING im_price = lo_discount->apply_discount(
im_price = 500
im_discount = 10 ) im_tax_rate = 5 ) ).

write ‘Final price with method chaining ‘,lv_final_price.

 

 

We can notice in the above example, 3 lines of code and 3 helper variables are converted to 1 method chain and 1 return variable. 

Method Chaining Structure: 

Innermost Call (apply_discount): 
Calculates the discounted price: 500 – (500 * 10 / 100) = 450. Middle Call (apply_tax): 
Applies tax to the discounted price: 450 + (450 * 5 / 100) = 473. Outermost Call (calculate_final_price): 
Returns the final calculated price: 473. 

 Important Points

For Method chaining, the system would start processing from the Inner Most method call to outer most method. Chained Method Call Requires Functional Methods i.e they must have a RETURNING parameter .
The Accepting Method Should Have Importing Parameters If there are multiple importing parameters, they should be optional so the system can decide which parameter to use when chaining or need to use the EXPORTING keyword to make parameter assignments clear.The RETURNING parameter from one method must match the type of the importing parameter in the next method.
If the types are different but compatible, the system will automatically
convert the value. 
In dynamic method calls, parameter handling is slower because the system dynamically determines the method at runtime. 
Ex: CALL METHOD lo_obj->(method_name) 
          EXPORTING  
             im_price 1000 
             im_discount 10 
        IMPORTING  
            re_discounted_price lv_final_price.  
 
SAP Recommendation: Use helper variables to store intermediate results instead of chaining methods dynamically. 
Ex: DATAlv_discounted_price TYPE i. 
lv_discounted_price lo_sales->apply_discount 
                        im_price 1000  
                        im_discount 10 ). 

  Debugging: 

In Debugging we have a tab called Auto in variable display tool. This tab would show you the step details whenever a method chain would be executed 

When you execute the statement using F6, you would notice 3 steps entry created in the Auto tab.
We can see here, system has started processing from the Inner Most method call to outer most    method.
 

 

Conclusion 
Method chaining in ABAP is a powerful technique for writing clean and modern code. It enhances readability and aligns with best practices in object-oriented programming. Whether you’re building complex queries, configuring objects, or designing APIs, method chaining can simplify your ABAP code and improve its maintainability.

 

​ What Is Method Chaining?  Method Chaining in ABAP allows you to call multiple methods one after the other in a single statement, without needing helper variables. This makes your code cleaner and easier to read. Previously, ABAP only supported chaining of attributes in class calls. But starting with ABAP 7.0 EhP2, you can also chain methods along with attributes.   Key Features of Method Chaining in SAP ABAP Chained Execution: Allows multiple methods to be executed in a single statement. Fluent Interface Design: Methods return the same object (SELF), enabling seamless chaining of operations. Enhanced Readability: Eliminates the need for intermediate variables, making the code cleaner and more intuitive. Concise Code: Reduces the number of lines of code, improving maintainability. Modern ABAP Practice: Aligns with modern programming paradigms and best practices.Ease of Debugging: Individual methods can be tested independently while supporting chaining.Advantages of Method Chaining Improved Readability: Code is more concise and easier to understand. Fewer Variables: Reduces the need for intermediate variables.Aligned with OOP: Encourages a clean and modern ABAP development style. Reusable Code: Enables building reusable and modular classes.Fluent Interface: Helps developers create a more intuitive and fluent API for their classes. Example for Method Chaining: I have taken 3 classes and one method in each class and given implementations for all three methods of a Class.   REPORT zch_rp_method_chaining.
CLASS lcl_discount DEFINITION.
PUBLIC SECTION.
METHODS apply_discount IMPORTING im_price TYPE i
im_discount TYPE i
RETURNING VALUE(re_discounted_price) TYPE i.
ENDCLASS.
CLASS lcl_tax DEFINITION.
PUBLIC SECTION.
METHODS apply_tax IMPORTING im_price TYPE i
im_tax_rate TYPE i
RETURNING VALUE(re_price_with_tax) TYPE i.
ENDCLASS.
CLASS lcl_final_price DEFINITION.
PUBLIC SECTION.
METHODS calculate_final_price IMPORTING im_price TYPE i
RETURNING VALUE(re_final_price) TYPE i.
ENDCLASS.

CLASS lcl_discount IMPLEMENTATION.
METHOD apply_discount.
re_discounted_price = im_price – ( im_price * im_discount / 100 ).
ENDMETHOD.
ENDCLASS.
CLASS lcl_tax IMPLEMENTATION.
METHOD apply_tax.
re_price_with_tax = im_price + ( im_price * im_tax_rate / 100 ).
ENDMETHOD.
ENDCLASS.
CLASS lcl_final_price IMPLEMENTATION.
METHOD calculate_final_price.
re_final_price = im_price.
ENDMETHOD.
ENDCLASS.  Without Method Chaining if we want to call the method :We need to create Object reference for each class and using that reference we need to call the method.   apply_discount Method: Accepts two parameters and returns a result.  apply_tax Method: Takes the result of  apply_discount method as input for im_price .  calculate_final_price : Uses the result of apply_tax as input for im_price and returns the final output  START-OF-SELECTION.
data : lo_discount type REF TO lcl_discount,
lo_tax type ref to lcl_tax,
lo_final_price type ref to lcl_final_price.

data : lv_discounted_price type i,
lv_price_with_tax type i,
lv_final_price type i.

CREATE OBJECT : lo_discount,
lo_tax,
lo_final_price.

“without method chaining
lv_discounted_price = lo_discount->apply_discount(
im_price = 500
im_discount = 10
).
lv_price_with_tax = lo_tax->apply_tax(
im_price = lv_discounted_price
im_tax_rate = 5
).
lv_final_price = lo_final_price->calculate_final_price( im_price = lv_price_with_tax ).

write :/’Final price without method chaining ‘,lv_final_price. With Method Chaining  “With method chaining
lv_final_price = lo_final_price->calculate_final_price(
EXPORTING im_price = lo_tax->apply_tax(
EXPORTING im_price = lo_discount->apply_discount(
im_price = 500
im_discount = 10 ) im_tax_rate = 5 ) ).

write ‘Final price with method chaining ‘,lv_final_price.  We can notice in the above example, 3 lines of code and 3 helper variables are converted to 1 method chain and 1 return variable. Method Chaining Structure: Innermost Call (apply_discount): Calculates the discounted price: 500 – (500 * 10 / 100) = 450. Middle Call (apply_tax): Applies tax to the discounted price: 450 + (450 * 5 / 100) = 473. Outermost Call (calculate_final_price): Returns the final calculated price: 473.  Important PointsFor Method chaining, the system would start processing from the Inner Most method call to outer most method. Chained Method Call Requires Functional Methods i.e they must have a RETURNING parameter .The Accepting Method Should Have Importing Parameters , If there are multiple importing parameters, they should be optional so the system can decide which parameter to use when chaining or need to use the EXPORTING keyword to make parameter assignments clear.The RETURNING parameter from one method must match the type of the importing parameter in the next method.If the types are different but compatible, the system will automatically convert the value. In dynamic method calls, parameter handling is slower because the system dynamically determines the method at runtime. Ex: CALL METHOD lo_obj->(method_name)           EXPORTING               im_price = 1000              im_discount = 10         IMPORTING              re_discounted_price = lv_final_price.   SAP Recommendation: Use helper variables to store intermediate results instead of chaining methods dynamically. Ex: DATA: lv_discounted_price TYPE i. lv_discounted_price = lo_sales->apply_discount(                          im_price = 1000                          im_discount = 10 ).   Debugging: In Debugging we have a tab called Auto in variable display tool. This tab would show you the step details whenever a method chain would be executed When you execute the statement using F6, you would notice 3 steps entry created in the Auto tab.We can see here, system has started processing from the Inner Most method call to outer most    method.  Conclusion Method chaining in ABAP is a powerful technique for writing clean and modern code. It enhances readability and aligns with best practices in object-oriented programming. Whether you’re building complex queries, configuring objects, or designing APIs, method chaining can simplify your ABAP code and improve its maintainability.   Read More Application Development Blog Posts articles 

#SAP

You May Also Like

More From Author