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.

Object-Oriented Programming 2.7

In 7.4, you can do this all in one line by using the CAST constructor operator.

DATA(lt_components) = CAST cl_abap_structdescr(

cl_abap_typedescr=>describe_by_name( 'ZSC_MONSTER_HEADER' ) )->

components.

Listing 2.70 Components of a Specific Dictionary Structure with CAST

The code in Listing 2.69 and Listing 2.70 performs exactly the same function, but

in the latter case you no longer need the helper variable LO_STRUCTDESCR and you

also do not need the line where you define the type LT_COMPONENTS.

2.7.2 CHANGING and EXPORTING Parameters

In ABAP, a functional method has until now been defined as a method with one

returning parameter and zero to many importing parameters, such as the following:

ls_monster_header = lo_monster->get_details( ld_monster_number ).

Many ABAP programmers liked the fact that you could put the result variable at

the start rather than having to put that variable in an EXPORTING parameter. However,

they wanted to be able to also have CHANGING and EXPORTING parameters as

well—that is, to have their cake and eat it too.

In 7.40, SAP has waved its magic wand, and now you can have it both ways. An

example is shown in Listing 2.71.

ls_monster_header = lo_monster->get_details(

EXPORTING id_monster_number = ld_monster_number

IMPORTING ed_something_spurious = ld_something_spurious

CHANGING cd_something_unrelated = ls_something_unrelated).

Listing 2.71 CHANGING and EXPORTING Parameters

There is no doubt many people will be happy with this, but purists who are used

to other languages will be horrified. (Although I do not feel quite that strongly, I

can see their point.) Good OO design leads you toward small methods that do one

thing, and the one thing for functional methods is to output one result. If a functional

method suddenly starts giving you back all sorts of other exporting parameters

and changes something else, then the method is clearly doing more than

one thing, and that is probably bad design. (For example, there are methods that

are designed to return four values of a polynomial equation, and you could use

127

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

Saved successfully!

Ooh no, something went wrong!