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
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:
Important Points
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.