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?

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)

ABAP Deep Dive - FOR (Loops)

Category - ABAP

Let's take a closer look at the FOR loop. How does it work? What do I have to consider and what can I do with it?

04/14/2023

ABAP Deep Dive - Table access (internal)

Category - ABAP

In this article, let's take a look at table access to internal tables and how they replace READ TABLE.

02/03/2023

ABAP Developer still relevant

Category - ABAP

In this article we look at whether ChatGPT can already replace an ABAP developer or whether it can be used as a help in everyday life.

01/06/2023

ABAP - FINAL

Category - ABAP

In this article, we'll take a look at the new FINAL language construct, how it works, and what you can do with it.

12/23/2022

ABAP Deep Dive - VALUE

Category - ABAP

In this article we want to look at the value statement again in all its forms and how you can use it in your daily work.

11/11/2022