Anthony Cecchini is the President of Information Technology Partners (ITP), an SAP consulting company headquartered in Pennsylvania. ITP offers comprehensive planning, resource allocation, implementation, upgrade, and training assistance to companies. Anthony has over 20 years of experience in SAP business process analysis and SAP systems integration. His areas of expertise include SAP NetWeaver integration; ALE development; RFC, BAPI, IDoc, Dialog, and Web Dynpro development; and customized Workflow development. You can reach him at firstname.lastname@example.org.
The continuing theme in all of these posts, is how we can make our code thinner, more readable and transparent. Well, nothing clogs up the visual readability of your code like long IF-THEN-ELSE-ENDIF or CASE-WHEN-ENDCASE constructs. Well, in ABAP 7.4 we have some new Constructor Operators we can use in Constructor Expressions that will make our code easier to read, more compact, and safer to maintain.
You remember Constructor Operators… We discussed them in the last blog post New Features in ABAP 7.4 – Calling Methods and Functions. In That post we highlighted the CONV Constructor Operator and how we could use it to convert a local variable to a STRING. In this post, I’d like to talk about the Constructor Operators COND and SWITCH. Again, for a complete list see the SAP ABAP 7.4 help for more information.
Since CASE statements can only evaluate one variable at a time, we use the IF/ELSE construct when we need to check multiple conditions. Look at the example below…
DATA: lv_text(30). IF lv_vehicle = '01' AND lv_type = 'C'. lv_text = 'Toyota'. ELSE. IF lv_vehicle ='02' AND lv_type = 'C'. lv_text = 'Chevy' ELSE. IF lv_vehicle ='03' AND lv_type = 'C'. lv_text = 'Range Rover'. .. ENDIF.
In ABAP 7.4, you can achieve the same thing, but you can do this in a more economical way by using the COND constructor operator. This also means that you do not have to keep specifying the target variable again and again. We will also add an ABAP INLINE Declaration… by using the DATA statement to create the variable lv_text inline on the fly!
DATA(lv_text) = COND text30( WHEN lv_vehicle ='01' AND lv_type = 'C' THEN 'Toyota' WHEN lv_vehicle ='02' AND lv_type = 'C' THEN 'Chevy' WHEN lv_vehicle ='03' AND lv_type = 'C' THEN 'Range Rover').
Here, we’re getting the day of the week and using a CASE statement to turn the number into a string, such as “Monday”, to output at the top of a report.
data: l_indicator like scal-indicator, l_day(10) type c. call function 'DATE_COMPUTE_DAY' exporting date = p_date importing day = l_indicator. case l_indicator. when 1. l_day = 'Monday'. when 2. l_day = 'Tuesday'. when 3. l_day = 'Wednesday'. when 4. l_day = 'Thursday'. when 5. l_day = 'Friday'. when 6. l_day = 'Saturday'. when 7. l_day = 'Sunday'. else. Raise exception type zcx_day_problem. endcase.
With ABAP 7.4, we can simplify and accomplish the same thing by by using the new SWITCH constructor operator instead of the CASE sttatement. Like so…
DATA(L_DAY) = SWITCH char10( l_indicator when 1 THEN 'Monday' when 2 THEN 'Tuesday' when 3 THEN 'Wednesday' when 4 THEN 'Thursday' when 5 THEN 'Friday' when 6 THEN 'Saturday' when 7 THEN 'Sunday' ELSE THROW zcx_day_problem( ) ).
Also note that if you are tired of only catching exceptions you can throw exceptions now! The usage is identical to the RAISE EXCEPTION TYPE, however, the compiler evaluates the keywords RAISE EXCEPTION TYPE and THROW as if they were one and the same.
For quite some time the lack of a real Boolean type in ABAP led to a lot of discussions amongst developers. I would see post on the SCN community constantly asking why a developer couldn’t write..
IF meth( … ). … ENDIF.
The answer from SAP was always “Because you have to have a Boolean type behind IF and no method can return a Boolean type.” But, now in Release 7.40, SP08 You can write predicative method calls as shown below..
… meth( ) …
as a short form of the relational expression
… meth( ) IS NOT INITIAL …
So you can place simple functional method calls everywhere, where logical expressions are allowed: behind IF, CHECK, ASSERT, COND, SWITCH, … Here is an example…
IF zcl_system=>is_production( ). "In production we never want a short dump TRY. zcl_application=>main( ). CATCH cx_sy_no_handler INTO gcl_no_handler. ENDTRY.
The reason this works is spelled out completely in Paul Hardy’s Book ABAP to The Future. Paul explains, “What’s happening from a technical point of view is that if you don’t specify anything after a functional method the compiler evaluates it as IS_PRODUCTION( ) IS NOT INITIAL. An ABAP_TRUE value is really the letter X, so the result is not initial, and so the statement is resolved as true.”
Another common situation with respect to Boolean logic in ABAP, is when you want to send a TRUE/FALSE value to a method or get such a value back from a functional method. For instance, did you ever stumble over this one?
IF boolc( 1 = 2 ) = ABAP_FALSE. WRITE: / 'YES'. ELSE. WRITE: / 'NO'. ENDIF.
Guess what? The relational expression “boolc( 1 = 2 ) = ABAP_FALSE” is false! Why? Because BOOLC despite its name does not return c but a string and the comparison rules for c and string blah, blah, blah …
Technically BOOLC( 1 = 2) evaluates to a string containing a blank of length 1. So far so good. But comparing ` ` with ‘ ‘ or ABAP_FALSE is false, since the text field is converted to string resulting in an empty string.
Now in ABAP 7.4, a new built-in function was added, called XSDBOOL, which does the same thing as BOOLC but returns an ABAP_BOOL type parameter.
IF xsdbool( 1 = 2 ) = ABAP_FALSE. WRITE: / 'YES'. ELSE. WRITE: / 'NO'. ENDIF.
The relational expression xsdbool( 1 = 2 ) = abap_false is true, because xsdbool returns type XSDBOOLEAN from the ABAP Dictionary that is – yes, you guess it – c of length 1. For the experts among you, XSDBOOLEAN is normally used for special mappings in XML-transformations and was reused here for quite another purpose. And that’s were the funny name xsdbool comes from.
Again, I highly recommend reading Horst Keller’s Blog on all the ABAP 7.4 enchantments to the code base.
With the new conditional code constructs in ABAP 7.2 and ABAP 7.4 we have the ability to create ABAP code with fewer statements for the same functionality, without compromising readability of the code. New boolean functions like XSDBOOL, and new conditional operators such as SWITCH and COND allow us more flexability and extensibility when developing and maintaining our ABAP code.