Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
horst_keller
Product and Topic Expert
Product and Topic Expert

In workshop CD261 at this years TechEds you will get your hands on expression enabled ABAP 7.40.

As almost every year the question arised, what kind of exercise can we offer to you, Two years ago, I prepared a pocket calculator simulation that was written in old style code and that the participants had to refactor to expression oriented code as far as possible with ABAP 7.02. Since gamification is such a buzz word and since another subject of the workshop - ABAP Channels - is also exemplified by a game, we (holger.janz and myself) have decided to also use a gaming example for expression enabled ABAP this year. We searched for an existing game programmed in ABAP and prepared it in such a way that it is usable for an exercise.

The existing game

As a suitable game we found a Jawbreaker adaption in enno.wulffTrick Tresor:  Well, if you look at the code, you see typical ABAP as you can find tons of it in real life programs: full of forms, OCCURS, WRITEs, ... (and comments and names in German) but no offense!. We like the idea of the game but it cannot be an exercise template in such a pre ABAP Objects state .

 

Refactoring the game for ABAP 7.0

Theerfore, we have rewritten the game in such a way that it represents a state-of-the-art 7.0 ABAP program. The new version uses ABAP Objects instead of subroutines of course and no other obsole statements any more. And since classical list programming is regarded as a kind of old fashioned, the game interface is realized as an HTML file shown in a browser control (but we will also offer a list based version):

You find the source code of the refactored game attached as Z_BLOCKS_HTML_700.txt. You can copy the code to your 7.02 or 7.0 sytem and run it happily there (since the convenience class CL_ABAP_BROWSER is not yet available in 7.0, you have to replace it with CL_GUI_HTML_VIEWER and use an own custom control there).  But the game is not the aim.

Express the game in 7.40

The aim is to beautify the ABAP code by replacing statements and helper variables with expressions whilst the functionality stays the same. This will be an exercise of the 2013 TechEd workshop. I could attach the full solution right now, but I won't (no cheating!). All I will say is that you can reduce the amount of lines by 35% by introducing expressions as shown in the following code snippets:

Example 1

Z_BLOCKS_HTML_700

DATA:
  rnd_color TYPE REF TO cl_abap_random_int,
  seed TYPE i.

seed = sy-uzeit.
rnd_color = cl_abap_random_int=>create( seed = seed min = 1 max = 4 ).

Z_BLOCKS_HTML_740

DATA(rnd_color) =
  cl_abap_random_int=>create( seed = CONV i( sy-uzeit ) min = 1 max = 4 ).

Example 2

Z_BLOCKS_HTML_700

READ TABLE field INDEX x ASSIGNING <column>.

READ TABLE <column> INDEX y ASSIGNING <color>.

Z_BLOCKS_HTML_740

DATA(color) = field[ x ][ y ].

Example 3

Z_BLOCKS_HTML_700

 

CASE <color>.

  WHEN 1.

    color = `lightblue`.

  WHEN 2.

    color = `cornflowerblue`.

  WHEN 3.

    color = `darkblue`.

  WHEN 4.

    color = `steelblue`.

ENDCASE.

DATA:

  xn TYPE n2,

  yn TYPE n2.

xn = x.

yn = y.

CONCATENATE

  html

  `<td bgcolor="` color `">`

  `<a href="sapevent:x` xn `y` yn

  `" style="text-decoration:none">` square `</a></td>`

INTO html.

Z_BLOCKS_HTML_740

html =

  html &&

  |<td bgcolor="{ SWITCH string( color  WHEN 1 THEN `lightblue`

                                        WHEN 2 THEN `cornflowerblue`

                                        WHEN 3 THEN `darkblue`

                                        WHEN 4 THEN `steelblue`) }">| &&

  |<a href="sapevent:x{ CONV n2( x )

                    }y{ CONV n2( y )

                    }" style="text-decoration:none">{ square }</a></td>|.

And of course, you can get your hands on ADT (Eclipse) for rewriting the code ...

See you at TechEd!

6 Comments
Labels in this area