Ternary operator (alternatives)

Is there a triple or conditional statement available in ABAP syntax? I did not find it, so I guess there is no answer, but is there an alternative that I can use to clear the usual "dumb" IF statements that I usually use?

For example, consider a method that logs a message with optional message parameters. To choose between an imported parameter or a default value, I have to check the value as follows:

 IF iv_class IS INITIAL. lv_message_class = 'DEFAULT'. ELSE. lv_message_class = iv_class. ENDIF. IF iv_number IS INITIAL. lv_message_number = '000'. ELSE. lv_message_number = iv_number. ENDIF. IF iv_type IS INITIAL. lv_message_type = 'E'. ELSE. lv_message_type = iv_type. ENDIF. 

The ternary operator will reduce each of these five-line operators to one line, as shown in the lower code block. This may even make the temporary variable unnecessary when the statement is used in a string.

 lv_message_class = iv_class IS INITIAL ? 'DEFAULT' : iv_class. lv_message_number = iv_number IS INITIAL ? '000' : iv_number . lv_message_type = iv_type IS INITIAL ? 'E' : iv_type . 

Is there any way to bring this programming style closer to ABAP or am I stuck with a mess?

+8
sap abap
source share
3 answers

Release 7.40 brings a whole bunch of ABAP enhancements that I find in interesting heaps. A triple style declaration (at least something similar to it) is one of them

Syntax:

 COND dtype|#( WHEN log_exp1 THEN result1 [ WHEN log_exp2 THEN result2 ] ... [ ELSE resultn ] ) ... 

An example of declaring data of a variable with the name "bool" and assigning a conditional value in one line. Old skool ABAP this will take 10 lines.

 DATA(bool) = COND #( WHEN i * i > number THEN abap_true ELSE abap_false ). 

Additional information: http://scn.sap.com/community/abap/blog/2013/07/22/abap-news-for-release-740

+19
source share

No, in ABAP there is no operator similar to a ? b : c a ? b : c , known from other languages. However, in your specific example, you can declare default values ​​for the parameters of your iv_class method, etc. In the method signature.

+2
source share

When declaring variables, you can set a default value or explicitly do the same as below.

 lv_message_class = 'DEFAULT'. lv_message_number = '000'. lv_message_type = 'E'. IF iv_class IS NOT INITIAL. lv_message_class = iv_class. ENDIF. IF iv_number IS NOT INITIAL. lv_message_number = iv_number. ENDIF. IF iv_type IS NOT INITIAL. lv_message_type = iv_type. ENDIF. 
+1
source share

All Articles