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

ABAP - Comparison

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?

Advertising

Again and again we see the use of the old operators in code reviews or that literals are still used for True/False. In today's article we want to go into the current standards and new commands in this environment.

 

Comparison operators

Many developers still use the classic ABAP comparison operators such as EQ, NE, GT, GE, LT or LE. In the meantime, however, the "normal" comparison operators (=, <>,>,> =, <, <=) have been used, since these are simple and clear for comparisons and database access. This also increases readability somewhat when used to differentiate from keywords and make a clear comparison. This method also makes it easier for younger developers who have previously gained experience in JAVA or Python to get started.

But there are also operators that can be used, for example, if you want to check whether a string is contained in a character string (CS) or a pattern occurs in a character string (CP). Here it is still common to switch to the short notation.

 

Boolean

Officially, ABAP does not have a real Boolean data type, as it does in almost all other languages. The data type CHAR with one character length has been used for years. True corresponds to a capital "X" and False is shown initially or via the constant "space". Unfortunately, we see again and again that the method interfaces refer to CHAR1, or that comparisons are made against literals.

The ABAP type group has been around for a very long time; the corresponding data types can be found there. In the meantime, the type groups no longer have to be actively included in the coding, they are always available. The comparison is therefore ABAP_TRUE and ABAP_FALSE; these can be used for the comparisons. The data type ABAP_BOOL can be used to define variables and interface parameters. If you want a data element that is also used on screens, we recommend ABAP_BOOLEAN.

DATA:
  ld_compare TYPE abap_boolean.

IF ld_compare = abap_true.
  " Do Something
ENDIF.

 

XSDBOOL

The command is a new expression that can be used to make a comparison and thus obtain a true or false. In many situations, this can save some code or even break large queries into smaller pieces. In the following example there is a method that returns a Boolean value if entries are found in a table.

SELECT FROM zbs_dy_t001
  FIELDS bukrs, butxt, land1
  INTO TABLE @DATA(lt_company_codes).

" Old
IF sy-subrc = 0.
  rd_found = abap_true.
ELSE.
  rd_found = abap_false.
ENDIF.

" New
rd_found = xsdbool( sy-subrc = 0 ).

 

COND

The command is more of a replacement for the construct of If and Else If and can be used for simple tasks in which, for example, only one value is set. This allows you to present the whole construct in a more compact way and you also have the option of generating an exception if, for example, no case occurs and the value is incorrect.

DATA:
  ld_value  TYPE i VALUE 42,
  ld_result TYPE abap_boolean.

" Old
IF ld_value > 40.
  ld_result = abap_true.
ELSEIF ld_value > 0.
  ld_result = abap_false.
ELSE.
  RAISE EXCEPTION NEW cx_sy_arg_out_of_domain( ).
ENDIF.

" New
ld_result = COND #( WHEN ld_value > 40 THEN abap_true
                    WHEN ld_value > 0 THEN abap_false
                    ELSE THROW cx_sy_arg_out_of_domain( ) ).

 

SWITCH

The switch command fulfills the same function as the CASE and should already be known from languages such as PHP. Also a similar example as with COND, because basically a similar result comes out. As you have probably already noticed, the command can also be used for inline declaration.

DATA:
  ld_value  TYPE i VALUE 42,
  ld_result TYPE abap_boolean.

" Old
CASE ld_value.
  WHEN 42.
    ld_result = abap_true.
  WHEN 39.
    ld_result = abap_false.
  WHEN OTHERS.
    RAISE EXCEPTION NEW cx_sy_arg_out_of_domain( ).
ENDCASE.

" New
ld_result = SWITCH #( ld_value
                      WHEN 42 THEN abap_true
                      WHEN 39 THEN abap_false
                      ELSE THROW cx_sy_arg_out_of_domain( ) ).

 

Conclusion

A lot has happened in the comparison of ABAP and the language has been made a bit more modern. With all the shorter spellings, you should also pay attention to proper formatting, otherwise new source code can quickly become difficult to read, actually what you want to avoid with clean code also in ABAP.

 

Source:
ABAP Documentation - XSDBOOL
ABAP Documentation - COND
ABAP Documentation - SWITCH


Included topics:
New ABAPComparisonSWITCHCONDXSDBOOL
Comments (0)
Advertising

ABAP - Predicative Method Call

Category - ABAP

Due to the OO concept, own methods are usually used for complex queries. This article is about comparing the result from such methods.

05/14/2021

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 - 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