Replace Case with Switch in ABAP New Syntax



Replace Case with Switch in ABAP New Syntax

In below example, the day such as Monday, Tuesday.. are determined by the numerical value obtained from the function module DATE_COMPUTE_DAY.

As per the old syntax, the code is as follows.

DATA: lv_indicator LIKE scal-indicator, 
 lv_day(10) TYPE c. 

CALL FUNCTION 'DATE_COMPUTE_DAY' 
 EXPORTING 
 date = p_date 
 IMPORTING 
 day = lv_indicator. 

CASE lv_indicator. 
 WHEN 1. 
 lv_day = 'Monday'. 
 WHEN 2. 
 lv_day = 'Tuesday'. 
 WHEN 3. 
 lv_day = 'Wednesday'. 
 WHEN 4. 
 lv_day = 'Thursday'. 
 WHEN 5. 
 lv_day = 'Friday'. 
 WHEN 6. 
 lv_day = 'Saturday'. 
 WHEN 7. 
 lv_day = 'Sunday'. 
ELSE. 
 RAISE EXCEPTION TYPE zcx_day_problem. 
ENDCASE.

With ABAP 7.4/7.5 new syntax feature the same code can be written as

DATA(lv_day) = SWITCH char10( lv_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( )

In the above code, repeated assignment of lv_day is avoided and only it is declared only once using the inline declaration.

Post a Comment

Previous Post Next Post