
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?
Table of contents
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