ABAP - Predicative Method Call
Due to the OO concept, own methods are usually used for complex queries. This article is about comparing the result from such methods.
Today we are going to introduce you to the last item from our statement list and thus close the chapter "New ABAP". This is about a shortened form of comparisons in terms of methods. In this article we will show you how you can use such comparisons effectively.
In order to be able to use the Predicative Method Call, you first need a clearly defined method that only has import and returning parameters. You should use such methods in most cases, as they provide a very short notation and are considered to be the most effective in the OO context. To do this, we define a class with three methods that make various things available to us.
CLASS zcl_test_predicative_call DEFINITION PUBLIC FINAL CREATE PUBLIC. PUBLIC SECTION. INTERFACES if_oo_adt_classrun. PROTECTED SECTION. PRIVATE SECTION. DATA: mt_list TYPE string_table. METHODS: is_item_in_list IMPORTING id_index TYPE i RETURNING VALUE(rd_result) TYPE abap_bool, has_user_authority RETURNING VALUE(rd_result) TYPE abap_bool, select_company_text IMPORTING id_company_code TYPE t001-bukrs RETURNING VALUE(rd_result) TYPE t001-butxt. ENDCLASS. CLASS zcl_test_predicative_call IMPLEMENTATION. METHOD if_oo_adt_classrun~main. ENDMETHOD. METHOD has_user_authority. AUTHORITY-CHECK OBJECT 'S_TCODE' ID 'TCD' FIELD 'ZDUMMY'. IF sy-subrc = 0. rd_result = abap_true. ELSE. rd_result = abap_false. ENDIF. ENDMETHOD. METHOD is_item_in_list. TRY. DATA(ls_line) = mt_list[ id_index ]. rd_result = abap_true. CATCH cx_sy_itab_line_not_found. rd_result = abap_false. ENDTRY. ENDMETHOD. METHOD select_company_text. SELECT SINGLE butxt FROM t001 WHERE bukrs = @id_company_code INTO @rd_result. ENDMETHOD. ENDCLASS.
In our example we have implemented three common patterns:
- IS_ - It is checked whether a certain state is present. There is a line in a table or the setting is set accordingly.
- HAS_ - It is checked whether someone or something has a certain property, for example a certain authorization for something.
- SELECT_ - The method selects something from the database and returns the value or the data.
Now that we have defined the class and our test methods, you can use them for the comparison. This special type of call always checks whether the return value of the method is not initial. To do this, we would check with IS NOT INITIAL or against the corresponding value. Here is an example from the classic world:
" old version IF is_item_in_list( 1 ) = abap_false. " ... ENDIF. IF select_company_text( '1234' ) IS NOT INITIAL. " ... ENDIF. IF has_user_authority( ) = abap_true. " ... ENDIF.
The new notation is now correspondingly shorter because you can omit the comparison.
" new version IF NOT is_item_in_list( 1 ). " ... ENDIF. IF select_company_text( '1234' ). " ... ENDIF. IF has_user_authority( ). " ... ENDIF.
In the example above you can also see that you can work with NOT if you want to check the opposite. In the first example we always want to run through the IF loop if there is no first entry in the list and so the data would have to be buffered. During the check, the correct initial value of the data type is always checked. Such initial values would be, for example:
- String = ``
- CHAR = ''
- NUMC2 = 00
- Integer = 0
- ABAP_BOOL = '' or ABAP_FALSE
- Object = NOT BOUND
Hint: The form of the short comparison can only be used for method calls, not for normal comparisons of variables and expressions.
You can find out more about method design in the official style guide from SAP on GitHub. All points of the language and also the design of ABAP source code are dealt with there. As a developer, you will find everything you need to write modern and clean source code there. In our examples, we have made the following adjustments in contrast to classic ABAP:
- Omission of CALL METHOD and use of brackets
- Omission of EXPORTING when calling if only IMPORTING and RETURNING are used in the method
- Omission of the parameter name if only one parameter is passed to the interface
- Use of the method directly in the query
- Call on one line
You see, with the new rules you can save a lot of lines of code and commands, it becomes clearer, easier to read and also to debug.
" old version DATA: ld_text TYPE t001-butxt. CALL METHOD select_company_text EXPORTING id_company_code = '1234' RECEIVING rd_result = ld_text. IF ld_text IS NOT INITIAL. " ... ENDIF. " new version IF select_company_text( '1234' ). " ... ENDIF.
The new variant of comparisons is shorter and easier to write, but requires a standardized method. We recommend that you use this form, of course only if everyone on your team understands the meaning and function of this call.