This is a test message to test the length of the message box.
Login
ABAP Predicative Method Call
Created by Software-Heroes

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.

Advertising

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.

 

Pre-Condition

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.

 

Usage

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.

 

Design

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.

 

Conclusion

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.

 

Source:
SAP Documentation - Predicative Method Call


Included topics:
New ABAPPredicative Method Call
Comments (0)
Advertising

ABAP - Performance for the SELECT

Category - ABAP

In this article we will look at a few special cases with the SELECT and examine the performance of these constructs. We'll show you the current alternatives and give you little tips while reading.

04/02/2021

ABAP - Check objects (Instances)

Category - ABAP

In this article we will show you how you can analyze instances and react to them correctly, for example if you hand them over during processing and want to react individually.

03/19/2021

ABAP - Loops

Category - ABAP

With New ABAP, new loops and possibilities for restricting table contents were created. We'll take a closer look at these new commands in this article.

02/19/2021

ABAP - Comparison

Category - ABAP

Today we'll look at comparisons and comparison operators in terms of the new commands and their current usage. What has changed so far and what should still be done?

02/12/2021

ABAP - String Functions (Part 3)

Category - ABAP

This article is about other new string functions and a replacement for CONDENSE. We would like to show you why you should use the new functions.

01/22/2021