New feature of ABAP Method Chaining is available from ABAP Release 7.0 EhP2 onward. Method chaining reduces the need for Helper variables, which makes code more cleaner.
What is method Chaining
You can now directly pass the returning value of one method call to a next method call without using any helper variable. This is referred as method chaining. Previously it was only allowed to create a chain of statements using attributes of the class. Now you can include methods as well as attributes in a chained call. Method chaining is available since ABAP Release 7.0 EhP2.
Don’t get it confused with the chained statements, which You write using colon symbol colon symbol ( : ) .
When using this chain of method feature, you would reduce lot of helper variables. Thus, you would improve code readability.
Code lines
Lets see the Method chaining example. In the example, I have used Chained Statements to instantiate the objects. I have also used the method calls Without using a method chaining, using helper variables. The next section of the code is showing you the method chaining which would replace the helper variables.
*&---------------------------------------------------------------------* *& Purpose : Method chaining example valid from ABAP 7.0 EhP2 *& Author : Naimesh Patel *& URL : http://zevolving.com/?p=1858 *&---------------------------------------------------------------------* REPORT ztest_np_method_chaining. * CLASS lcl_sum DEFINITION. PUBLIC SECTION. METHODS: do_sum IMPORTING i_1 TYPE i i_2 TYPE i RETURNING value(result) TYPE i. ENDCLASS. "lcl_sum DEFINITION * CLASS lcl_mul DEFINITION. PUBLIC SECTION. METHODS: do_mul IMPORTING i_1 TYPE i OPTIONAL i_2 TYPE i OPTIONAL PREFERRED PARAMETER i_2 RETURNING value(result) TYPE i. ENDCLASS. "lcl_mul DEFINITION * CLASS lcl_sub DEFINITION. PUBLIC SECTION. METHODS: do_sub IMPORTING i_1 TYPE i RETURNING value(result) TYPE i. ENDCLASS. "lcl_sub DEFINITION * START-OF-SELECTION. DATA: lo_sum TYPE REF TO lcl_sum. DATA: lo_mul TYPE REF TO lcl_mul. DATA: lo_sub TYPE REF TO lcl_sub. * * Chained Statement to instantiate the object CREATE OBJECT: lo_sum, lo_mul, lo_sub. * * Without method chaining DATA: lv_res_sum TYPE i. DATA: lv_res_mul TYPE i. DATA: lv_res_sub TYPE i. lv_res_sum = lo_sum->do_sum( i_1 = 5 i_2 = 10 ). lv_res_mul = lo_mul->do_mul( lv_res_sum ). lv_res_sub = lo_sub->do_sub( lv_res_mul ). WRITE: / lv_res_sub. * * Method Chaining DATA: lv_result TYPE i. lv_result = lo_sub->do_sub( lo_mul->do_mul( lo_sum->do_sum( i_1 = 5 i_2 = 10 ) ) ). WRITE: / lv_result. * * little bit complex chaining lv_result = lo_sub->do_sub( i_1 = lo_mul->do_mul( 5 ) * lo_mul->do_mul( i_1 = lo_sum->do_sum( i_1 = 5 i_2 = 10 ) ) ). WRITE: / lv_result. * CLASS lcl_sum IMPLEMENTATION. METHOD do_sum. result = i_1 + i_2. ENDMETHOD. "do_Sum ENDCLASS. "lcl_sum IMPLEMENTATION * CLASS lcl_mul IMPLEMENTATION. METHOD do_mul. " I_1 would be initial here as its not PREFERRED result = i_1 * 2. result = i_2 * 4. ENDMETHOD. "do_mul ENDCLASS. "lcl_mul IMPLEMENTATION * CLASS lcl_sub IMPLEMENTATION. METHOD do_sub. result = i_1 - 3. ENDMETHOD. "do_Sub ENDCLASS. "lcl_sub IMPLEMENTATION
As you can notice in the example, 3 lines of code and 3 helper variables are converted to 1 method chain and 1 return variable.
Few Points to keep in mind
When trying to use method chaining, you should keep these points in mind:
- For Method chaining, system would start processing from the Inner Most method call to outer most method.
- For a chained method call the method which is being chained, should be functional method. In other words, it must have returning parameter e.g. DO_SUM & DO_SUB.
- The method which is accepting this result value should have one importing parameter. If more than one importing parameter, they should be Optional. One of those parameters should be defined as PREFERRED Parameter e.g. DO_SUB
- When Types of Returning parameter is different than the Importing parameter, System would try to convert the value into importing parameter.
- When Returning parameter type is not compatible with the importing parameter, syntax check would occur like a normal method call.
- For Dynamic Method calls – where method is called like
call method c->(meth_name)
, the handling of the Parameters is time consuming, so it is suggested by SAP help to use the helper variables instead of the functional method chaining.
Debugging through method chaining
Since SAP NetWeaver 7.0 EhP2, you would also notice a tab called Auto in variable display tool. This tab would show you the step details whenever a method chain would be executed.
For statement:
lv_result = lo_sub->do_sub( lo_mul->do_mul( lo_sum->do_sum( i_1 = 5 i_2 = 10 ) ) ).
When you execute the statement using F6, you would notice 3 steps entry created in the Auto tab. As you can see here, system would start processing from the Inner Most method call to outer most method.
For another a little bit complex statement, the execution would be a bit different. So, the Entries in Auto tab would also be different.
* little bit complex chaining lv_result = lo_sub->do_sub( i_1 = lo_mul->do_mul( 5 ) * lo_mul->do_mul( i_1 = lo_sum->do_sum( i_1 = 5 i_2 = 10 ) ) ).
System would First Execute the Multiplication of the first part of the arithmetic equation. After that it would start processing second part of the multiplication equation.
Tip – if you reach directly to a statement after the method chaining, the Auto tab wont show any step processed. Because the step is already processed and Debugger didn’t care what values were returned by each step with the call.
You need to put break point on method chain and press F6 to execute the statement fully to see the values in Auto tab.
Ever since EHP2 – where possible my objects now have returing parameters rather than exporting/changing. It just makes coding so much easier and as you say reduces the number of helper variables. It is very rare that I have to code for an exporting parameter now.
Hello Nathan,
That’s very much true. You don’t need lot of Helper variables and code is cleaner and easier to maintain. For few instances where you really need to have changing parameter, like a method to update ITAB based on certain values.
Regards,
Naimesh Patel
Excellent Naimesh…Im a big follower of you…Thanks….