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

ABAP - Comparison

2224

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)



And further ...

Are you satisfied with the content of the article? We post new content in the ABAP area every Friday and irregularly in all other areas. Take a look at our tools and apps, we provide them free of charge.


ABAP - XCO UUID

Category - ABAP

How do you generate unique IDs in ABAP using the XCO classes? In this article, you will learn how quickly and easily this can be done.

01/28/2025

ABAP - XCO System Fields

Category - ABAP

Is there an alternative for SY or SYST in the XCO Library in ABAP Cloud? It's definitely worth a look.

01/24/2025

ABAP - XCO Strings

Category - ABAP

In this article we look at the XCO class for generating and processing strings for ABAP Cloud and compare it with the classic statements.

11/22/2024

ABAP - XCO Libraries

Category - ABAP

What can you do with the library in ABAP and ABAP Cloud and how do you use the objects the best way? Find out more here.

11/12/2024

ABAP - Type Casting

Category - ABAP

How do you actually get the original type of a class or instance if it is passed in a generic table? In this article we examine the possibilities.

04/16/2024