14.01.2020 Views

ABAP_to_the_Future

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

2

New Language Features in ABAP 7.4

2.5.5 The COND Statement as a Replacement for IF/ELSE

All throughout this book, you will be reading about how resistance to change is

bad. Despite that, let’s admit it: Change can be annoying, especially when it

affects your code. One great exam ple of this involves coding a CASE statement

based on the assumption that one value is derived from the value of another.

Then, someone comes along an d tells you that the rules have changed, and the

last value in the CASE statement is only true if it is a Tuesday. On Wednesday, the

value becomes something else.

CASE statements can only evaluate one variab le at a time, so, in the case of this

example, you have to change the whole thing into an IF/ELSE construct. That is

not the end of civilization as we know it, but the more changes you have to make,

the bigger the risk.

Say that you’re really scared that the logic you’ve been given might change at

some point in the future, but nonetheless you start off with a CASE, such as the

code in Listing 2.40, which is pre-7.4 code that evaluates the description of a

monster’s sanity based on a numeric value.

* Fill the Sanity Description

CASE cs_monster_header-sanity.

WHEN 5.

cs_monster_header-sanity_description = 'VERY SANE'.

WHEN 4.

cs_monster_header-sanity_description = 'SANE'.

WHEN 3.

cs_monster_header-sanity_description = 'SLIGHTLY MAD'.

WHEN 2.

cs_monster_header-sanity_description = 'VERY MAD'.

WHEN 1.

cs_monster_header-sanity_description = 'BONKERS'.

WHEN OTHERS.

cs_monster_header-sanity_description = 'RENAMES SAP PRODUCTS'.

ENDCASE.

Listing 2.40 CASE Statement to Evaluate Monster Sanity

In 7.4, you can achieve the same thing, but you can do this in a more compact

way by using the COND constructor operator. This also means that you do not have

to keep specifying the target variable again and again (see Listing 2.41).

* Fill the Sanity Description

cs_monster_header-sanity_description =

COND text30(

108

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!