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.

The What vs. the How A.2

When it comes to writing the code for how to do any given task, the audience is

quite different; this time, you’re talking to a machine. Obviously, a programmer

is still going to read that code as well and try to get his head around it, but at this

point you’re looking at the nuts and bolts of programming and can be as technical

as you want, as opposed to the abstract concepts talked about in the “what” context.

Here’s an example: In Chapter 2, you learned how the CAST expression could simplify

the code for building an internal table containing the components of a structure.

In traditional ABAP, the code was as follows:

DATA lo_structdescr TYPE REF TO cl_abap_structdescr.

lo_structdescr ?= cl_abap_typedescr=>describe_by_name( 'ZSC_MONSTER_

HEADER' ).

DATA lt_components TYPE abap_compdescr_tab.

lt_components = lo_structdescr->components.

As noted, 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.

The second block of code is certainly shorter, but is it less readable? In this case,

neither is particularly readable, but in some examples you have seen, the new

shortened code makes traditional ABAPers struggle, so much so that they might

not even be able to maintain code written by their more “thoroughly modern”

colleagues.

Look at this a different way: Why do you want the components? It turns out that

every field in this structure is on a DYNPRO screen, and you want to read all the

values in. We don’t want to do this one at a time, so you need the components so

that you can loop through them reading each value.

Robert Martin said that some programming languages force the following sort of

structure:

왘 To process the user input, we need the current screen values.

왘 To get the current screen values, we need to read them off the screen.

왘 To read the values of the screen, we need a list of fields to read.

왘 So forth and so on.

709

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

Saved successfully!

Ooh no, something went wrong!