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.

1

ABAP in Eclipse

gram fragility is duplicate code. It’s so easy to cut and paste a big lump of code

from one place to the other and just change the variables, leaving the logic exactly

the same. Before you know it, you have the same piece of code repeated all over

the program or, even worse, in many different programs.

Then the change comes: th e logic needs to be adjust ed. You now have to make

that change in 54 different places, and of course you are going to forget to make

the change in some of them. This can lead to unpredictable results. The user will

experience different behavior when doing the same thing in different contexts; in

other words, the change has snapped the program in two, like a glass twig being

jumped up on and down on by an elep hant during an earthquake. The obvious

solution is that when you want to copyand paste a chunk of code and make some

small changes you should extract the bulk of the code into its own method, with

IMPORTING parameters for the parts that vary. That sounds all fine and dandy in

principle, but it’s a bit of an effort, and it’s so much easier to highlight the code

and do the (CTRL) + (C)/(CTRL) + (V) trick, and people are in a hurry, so that’s what

they do, little realizing they’ve just made a rod for their own (and their colleagues’,

present and future) backs.

Fortunately, ABAP in Eclipse gives you a tool to automatically extract chunks of

code into their own methods. Listing 1.6 shows sample code that you might suddenly

realize you need in lots of different places, with the variables changed but

the logic the same.

CLASS lcl_monster IMPLEMENTATION.

METHOD main.

* Local Variables

DATA: ld_monster_madness1 TYPE i,

ld_monster_madness2 TYPE i,

ld_monster_madness3 TYPE i,

ld_description1 TYPE string,

ld_description2 TYPE string,

ld_description3 TYPE string.

ld_monster_madness1 = 25.

ld_monster_madness2 = 50.

ld_monster_madness3 = 100.

* Derive Monster Sanity

IF ld_monster_madness1 LT 30.

ld_description1 = 'FAIRLY SANE'.

ELSEIF ld_monster_madness1 GT 90.

54

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

Saved successfully!

Ooh no, something went wrong!