Improve Your Abap Code
-
Upload
pavan-hukkeri -
Category
Documents
-
view
232 -
download
1
Transcript of Improve Your Abap Code
8/9/2019 Improve Your Abap Code
http://slidepdf.com/reader/full/improve-your-abap-code 1/23
Improve Your ABAP CODE:
OVERVIEW
Today, I'm going to start a new series of posts about improving the readability of your code In this post
series, I will share you my top ! approaches that can help to ma"e your and your colleagues' life easier
In the past, I have seen many#many $%$& source codes with the lac" of readability These coders usually
choose poor names for their programs, classes, methods, subroutines, function modules, variables, and
so on, which ma"es their own and our business more complicated
I "now that in $%$&, we are a bit limited in the length of the identifiers, but usually we have enough
characters to epress our thoughts (depending on the type of the indentifiers) I don't thin" it belongs only
to these length limitations, I thin" it's more about your eactitude *on't be afraid using meaningful names+
%esides the $%$& development, I do many - development as well than"s to .amarin and our Website
In the - community, I have noticed that the coders ta"e more responsibilities for their codes than in
$%$& side, they care more about re#usability, transparency, /uality, and readability I believe that this is
the way, we should develop in $%$& as well
Let's take more responsibilities for our codes!
0$12 3T20E
First of all, I want to show you, how annoying an !e an ABAP soure o"e with poor
rea"a!ility# $et%s he& the following e'ample, an" answer the following (uestions:
• )hat is cl_ord1 a!out*
• )hat is its responsi!ility*
• )hat is calc_dp "oing*
• )hat "o ms, tb, an" dp mean*
I thin& these are all har" (uestion for you, if you aren%t the implementer of this o"e +may!e
even if you# -here are always meetings where the o"ers are e'plaining the !ehavior of
their o"e to the others# I thin& most of us have !een on suh a meeting, an" we an agree
in that they are mostly useless#
8/9/2019 Improve Your Abap Code
http://slidepdf.com/reader/full/improve-your-abap-code 2/23
)hat%s happen, if we get a o"e li&e this !elow, an" our tas& is to hange something in it,
!eause the !usiness logi has !een hange"# I thin& most of us starts srathing his.her
hea", an" spen"s many time to /gure out what the o"e is atually "oing !efore "o any
hanges# It%s a waste of time#
0y a"vie is that use meaningful names for your i"enti/ers, an" you an avoi" these
e'planation roun"s an" hea" srathing1
2
3
4
5
6
7
8
9
2;
22
23
24
25
26
27
28
29
2
3;
32
33
<======================================================================<
C$A>> l?or"2 DEFI@I-IO@ I@EI-I@ FO0 l?or"#
<======================================================================<
PB$IC >EC-IO@#
###
PIA-E >EC-IO@#
###
0E-OD> al?"p
I0PO-I@ ms -YPE string
t! -YPE string
E-@I@ value+"p -YPE i#
E@DC$A>># l?or"2 DEFI@I-IO@
<======================================================================<
C$A>> l?or"2 I0P$E0E@-A-IO@#
<======================================================================<
###
0E-OD al?"p#
IF ms G %;2% A@D t! G %C%#
"p G 4;#
E@DIF#
IF ms G %;% A@D t! G %-%#
"p G 26#
E@DIF#
8/9/2019 Improve Your Abap Code
http://slidepdf.com/reader/full/improve-your-abap-code 3/23
34
35
36
37
38
39
3
4;
42
43
44
45
IF ms G %;8% A@D t! G %)D%#
"p G 6#
E@DIF#
E@D0E-OD# al?"p
###
E@DC$A>># l?or"2 I0P$E0E@-A-IO@
43E 5E$6I67840 0$33 6$5E3
)hat if I say that it%s a Boo& Or"er, an" rename the lass to cl_book_order, an" its !ase
lass to cl_order# It%s more meaningful for me# )e have a !ase lass, cl_order, an" many
"erive" lasses, li&e cl_book_order for Boo&s, cl_mobile_order for 0o!ile "evies, orcl_garden_order for ar"en e(uipments# >o we apply "iHerent !usiness logi for the
"iHerent type of or"ers#
2
3
4
5
6
7
8
9
<======================================================================<
C$A>> l?!oo&?or"er DEFI@I-IO@ I@EI-I@ FO0 l?or"er#
<======================================================================<
PB$IC >EC-IO@#
###
PIA-E >EC-IO@#
###
0E-OD> al?"p
I0PO-I@ ms -YPE string
8/9/2019 Improve Your Abap Code
http://slidepdf.com/reader/full/improve-your-abap-code 4/23
2;
22
23
24
25
26
27
28
29
2
3;
32
33
34
35
36
37
38
39
3
4;
42
43
44
45
t! -YPE string
E-@I@ value+"p -YPE i#
E@DC$A>># l?!oo&?or"er DEFI@I-IO@
<======================================================================<
C$A>> l?!oo&?or"er I0P$E0E@-A-IO@#
<======================================================================<
###
0E-OD al?"p#
IF ms G %;2% A@D t! G %C%#
"p G 4;#
E@DIF#
IF ms G %;% A@D t! G %-%#
"p G 26#
E@DIF#
IF ms G %;8% A@D t! G %)D%#
"p G 6#
E@DIF#
E@D0E-OD# al?"p
###
E@DC$A>># l?!oo&?or"er I0P$E0E@-A-IO@
8/9/2019 Improve Your Abap Code
http://slidepdf.com/reader/full/improve-your-abap-code 5/23
43E 5E$6I67840 5ET9O* 6$5E3
)hat if I say that the responsi!ility of our metho" is to alulate the "isount perentage for
a !oo&, an" rename the metho" to calculate_discount_percentage# @ow, it ma&es more
sense for me, an" if someone as&s me what that metho" is "oing, I an say that it alulates
the "isount perentage pro!a!ly for a !oo&#
2
3
4
5
6
7
8
9
2;
22
23
24
25
26
27
28
29
2
3;
32
33
34
<======================================================================<
C$A>> l?!oo&?or"er DEFI@I-IO@ I@EI-I@ FO0 l?or"er#
<======================================================================<
PB$IC >EC-IO@#
###
PIA-E >EC-IO@#
###
0E-OD> alulate?"isount?perentage
I0PO-I@ ms -YPE string
t! -YPE string
E-@I@ value+"p -YPE i#
E@DC$A>># l?!oo&?or"er DEFI@I-IO@
<======================================================================<
C$A>> l?!oo&?or"er I0P$E0E@-A-IO@#
<======================================================================<
###
0E-OD alulate?"isount?perentage#
IF ms G %;2% A@D t! G %C%#
"p G 4;#
E@DIF#
IF ms G %;% A@D t! G %-%#
"p G 26#
E@DIF#
IF ms G %;8% A@D t! G %)D%#
8/9/2019 Improve Your Abap Code
http://slidepdf.com/reader/full/improve-your-abap-code 6/23
35
36
37
38
39
3
4;
42
43
44
45
"p G 6#
E@DIF#
E@D0E-OD# alulate?"isount?perentage
###
E@DC$A>># l?!oo&?or"er I0P$E0E@-A-IO@
43E 5E$6I67840 &$R$5ETER 6$5E3
)hat if I say that the metho" calculate_discount_percentage alulates the "isount
perentage !ase" on two parameters the month of the sales "ate an" the type of the given
!oo&, an" rename them to month_of_sales, type_of_book , an" discount_percentage#
@ow we an easily say that the the "isount perentage is alulate" in the following way:
• If the the month of the sales "ate is 01 an" the type of the !oo& is CH, then the
"isount perentage is 30,
• If the the month of the sales "ate is 09 an" the type of the !oo& is TR, then the
"isount perentage is 15,
• If the the month of the sales "ate is 0 an" the type of the !oo& is !", then the
"isount perentage is 5#
)e still "on%t now e'atly what the 01, 09, 0, CH, TR, or !" means, !ut I !elieve that we
got loser to more easily /gure out what this o"e is "oing#
2
3
<======================================================================<
C$A>> l?!oo&?or"er DEFI@I-IO@ I@EI-I@ FO0 l?or"er#
<======================================================================<
8/9/2019 Improve Your Abap Code
http://slidepdf.com/reader/full/improve-your-abap-code 7/23
4
5
6
7
8
9
2;
22
23
24
25
26
27
28
29
2
3;
32
33
34
35
36
37
38
39
3
4;
PB$IC >EC-IO@#
###
PIA-E >EC-IO@#
###
0E-OD> alulate?"isount?perentage
I0PO-I@ month?of?sales -YPE string
type?of?!oo& -YPE string
E-@I@ value+"isount?perentage -YPE i#
E@DC$A>># l?!oo&?or"er DEFI@I-IO@
<======================================================================<
C$A>> l?!oo&?or"er I0P$E0E@-A-IO@#
<======================================================================<
###
0E-OD alulate?"isount?perentage#
IF month?of?sales G %;2% A@D type?of?!oo& G %C%#
"isount?perentage G 4;#
E@DIF#
IF month?of?sales G %;% A@D type?of?!oo& G %-%#
"isount?perentage G 26#
E@DIF#
IF month?of?sales G %;8% A@D type?of?!oo& G %)D%#
"isount?perentage G 6#
E@DIF#
E@D0E-OD# alulate?"isount?perentage
###
E@DC$A>># l?!oo&?or"er I0P$E0E@-A-IO@
8/9/2019 Improve Your Abap Code
http://slidepdf.com/reader/full/improve-your-abap-code 8/23
42
43
44
45
3455$R2
I hope you oul" e'periene that it "oes really matter how "o you name your i"enti/ers in
your o"e, whether it%s a program, lass, metho", su!routine, funtion mo"ule, varia!le or a
onstant#
-hin& of your o"e as a !oo&, your olleagues as rea"ers, an" you are the writer who isresponsi!le to write lightweight soure o"e that everyone an easily rea" an" un"erstan"#
Of ourse, this was the /rst step of improving the rea"a!ility of our soure o"e, so there are
still many ways to improve it# >tay tune", &eep rea"ing1 @e't, we are going to tal& a!out the
magic numbers#
Choosing good names takes time but saves more than it takes#
Part 3:
OVERVIEW
After showing you the real !ene/ts of using meaningful names, I want to share you another
useful tip that an help you to improve the rea"a!ility of your o"e# sually, it%s nota"visa!le to use many har"=o"e" values in your soure o"e, !ut there are ases when you
nee" them# For e'ample, you want to alulate the total wor&ing hours of a month, !ase" on
the wor&ing "ays, using the following formula:
2 total?wor&ing?hours G wor&ing?"ays < 9#
8/9/2019 Improve Your Abap Code
http://slidepdf.com/reader/full/improve-your-abap-code 9/23
)e all this num!er a!ove in the formula, as #agic $umber# sing 0agi @um!ers an
ma&e our life more "iult, !eause
• we "on%t have any semanti information a!out the num!er 9 +we "on%t &now what it
means,
• it an inrease the maintenane osts, if we use it many times in our soure o"e,
• it an inrease the num!er of !ugs, if we use it many times in our soure o"e, an"
we ma&e a typo "uring the hanges#
>o instea" of using the num!er 9 in our formula, we intro"ue a new onstant, alle"
%orking_hours_in_a_day with the value &, an" replae all ourrenes of the num!er 9 to
this onstant in the soure o"e# )ith this improvement, you ma&e your o"e more
rea"a!le, re"ue the maintenane osts an" re"ue the num!er of the !ugs in one fellswoop#
2
3
4
CO@>-A@-> wor&ing?hours?in?a?"ay -YPE i A$E 9#
total?wor&ing?hours G wor&ing?"ays < wor&ing?hours?in?a?"ay#
Today's message: Be reative, "e/ne more meaningful onstants1
0$3T ITER$TIO6
As you remem!er, we have left our o"e in this state in the last iteration# It%s more rea"a!le
an" transparent than !efore, !ut it%s still hi"ing some information from us# )e still "on%t now
e'atly what the magial num!ers, 01, 09, 0, CH, TR, or !" means# )e an guess, !ut
we annot !e sure, for e'ample the CH means Children or Chemistry# >o let%s eliminate
these misun"erstan"ings using C($)T*$T)#
2
3
4
5
<======================================================================<
C$A>> l?!oo&?or"er DEFI@I-IO@ I@EI-I@ FO0 l?or"er#
<======================================================================<
PB$IC >EC-IO@#
###
8/9/2019 Improve Your Abap Code
http://slidepdf.com/reader/full/improve-your-abap-code 10/23
6
7
8
9
2;
22
23
24
25
26
27
28
29
2
3;
32
33
34
35
36
37
38
39
3
4;
42
43
PIA-E >EC-IO@#
###
0E-OD> alulate?"isount?perentage
I0PO-I@ month?of?sales -YPE string
type?of?!oo& -YPE string
E-@I@ value+"isount?perentage -YPE i#
E@DC$A>># l?!oo&?or"er DEFI@I-IO@
<======================================================================<
C$A>> l?!oo&?or"er I0P$E0E@-A-IO@#
<======================================================================<
###
0E-OD alulate?"isount?perentage#
IF month?of?sales G %;2% A@D type?of?!oo& G %C%#
"isount?perentage G 4;#
E@DIF#
IF month?of?sales G %;% A@D type?of?!oo& G %-%#
"isount?perentage G 26#
E@DIF#
IF month?of?sales G %;8% A@D type?of?!oo& G %)D%#
"isount?perentage G 6#
E@DIF#
E@D0E-OD# alulate?"isount?perentage
###
E@DC$A>># l?!oo&?or"er I0P$E0E@-A-IO@
8/9/2019 Improve Your Abap Code
http://slidepdf.com/reader/full/improve-your-abap-code 11/23
44
45
*E8I6E O63T$6T3 70O%$002 I6 T9E &$RE6T0$33
-here are many ways "e/ning onstants in ABAP, an" speially in OO ABAP# Both approahes
are goo", it only "epen"s on the re(uirements# )e an "e/ne them:
• in a glo!al lass,
• in a glo!al interfae,
• in the parent lass,
• within the same lass,
• an" so on#
)hat if I say that the magi num!ers 01, 09 an" 0 are represent months* Alright, you an
say that it%s not a !ig "eal# But if you have the tools to type +anuary, )eptember an" +uly
instea" of those magi num!ers, why woul"n%t you "o that*
>pea&ing a!out our e'ample o"e, we are going to "e/ne these onstants in the parent
class, un"er the ,R(T-CT-" )-CT.($, in or"er to share them among all the inherite"
lasses +sine it% not only !oo& relevant information#
-hen there is nothing left, than referring them as /anuary, september an" /uly in the
su!lass, cl_book_order# If the month of the sales "ate is e(ual to January### I%m a !ig fan of
o"e li&e this, it%s self=e'pressive1
2
3
4
5
6
<======================================================================<
C$A>> l?or"er DEFI@I-IO@#
<======================================================================<
PO-EC-ED >EC-IO@#
CO@>-A@-> January -YPE string A$E %;2%#
CO@>-A@-> July -YPE string A$E %;8%#
CO@>-A@-> septem!er -YPE string A$E %;%#
8/9/2019 Improve Your Abap Code
http://slidepdf.com/reader/full/improve-your-abap-code 12/23
7
8
9
2;
22
23
24
25
26
27
28
29
2
3;
32
33
34
35
36
37
38
39
3
4;
42
43
44
###
E@DC$A>># l?or"er DEFI@I-IO@
<======================================================================<
C$A>> l?!oo&?or"er DEFI@I-IO@ I@EI-I@ FO0 l?or"er#
<======================================================================<
PB$IC >EC-IO@#
###
PIA-E >EC-IO@#
###
0E-OD> alulate?"isount?perentage
I0PO-I@ month?of?sales -YPE string
type?of?!oo& -YPE string
E-@I@ value+"isount?perentage -YPE i#
E@DC$A>># l?!oo&?or"er DEFI@I-IO@
<======================================================================<
C$A>> l?!oo&?or"er I0P$E0E@-A-IO@#
<======================================================================<
###
0E-OD alulate?"isount?perentage#
IF month?of?sales G January A@D type?of?!oo& G %C%#
"isount?perentage G 4;#
E@DIF#
IF month?of?sales G septem!er A@D type?of?!oo& G %-%#
"isount?perentage G 26#
E@DIF#
IF month?of?sales G July A@D type?of?!oo& G %)D%#
"isount?perentage G 6#
E@DIF#
E@D0E-OD# alulate?"isount?perentage
8/9/2019 Improve Your Abap Code
http://slidepdf.com/reader/full/improve-your-abap-code 13/23
45
46
47
48
49
4
5;
52
53
54
55
###
E@DC$A>># l?!oo&?or"er I0P$E0E@-A-IO@
*E8I6E O63T$6T3 WIT9I6 T9E 0$33
)hat a!out the !oo& types* )hat if I say that the magi num!ers CH, TR, !" mean eah
of the followings: Children, Trael an" !eb "esign*
@ow, we are tal&ing a!out !oo& type that is a !oo& relevant information, so we an simply
"e/ne them within the same lass, cl_book_order, an" referring them in the
implementation setion as Children, Trael an" !eb "esign#
Alright, the /rst !ranh soun"s li&e this: if the month of the sales date is e(ual to
/anuary, an" the type of the book is e(ual to children, then discount percentage is
going to !e 30# )hat "o you thin&* Is it !etter* @o e'planation, no hea" srathing1
2
3
4
5
6
7
<======================================================================<
C$A>> l?or"er DEFI@I-IO@#
<======================================================================<
PO-EC-ED >EC-IO@#
CO@>-A@-> January -YPE string A$E %;2%#
CO@>-A@-> July -YPE string A$E %;8%#
CO@>-A@-> septem!er -YPE string A$E %;%#
8/9/2019 Improve Your Abap Code
http://slidepdf.com/reader/full/improve-your-abap-code 14/23
8
9
2;
22
23
24
25
26
27
28
29
2
3;
32
33
34
35
36
37
38
39
3
4;
42
43
44
45
###
E@DC$A>># l?or"er DEFI@I-IO@
<======================================================================<
C$A>> l?!oo&?or"er DEFI@I-IO@ I@EI-I@ FO0 l?or"er#
<======================================================================<
PB$IC >EC-IO@#
###
PIA-E >EC-IO@#
CO@>-A@-> hil"ren -YPE string A$E %C%#
CO@>-A@-> travel -YPE string A$E %-%#
CO@>-A@-> we!?"esign -YPE string A$E %)D%#
###
0E-OD> alulate?"isount?perentage
I0PO-I@ month?of?sales -YPE string
type?of?!oo& -YPE string
E-@I@ value+"isount?perentage -YPE i#
E@DC$A>># l?!oo&?or"er DEFI@I-IO@
<======================================================================<
C$A>> l?!oo&?or"er I0P$E0E@-A-IO@#
<======================================================================<
###
0E-OD alulate?"isount?perentage#
IF month?of?sales G January A@D type?of?!oo& G hil"ren#
"isount?perentage G 4;#
E@DIF#
IF month?of?sales G septem!er A@D type?of?!oo& G travel#
"isount?perentage G 26#
E@DIF#
8/9/2019 Improve Your Abap Code
http://slidepdf.com/reader/full/improve-your-abap-code 15/23
46
47
48
49
4
5;
52
53
54
55
56
57
58
59
IF month?of?sales G July A@D type?of?!oo& G we!?"esign#
"isount?perentage G 6#
E@DIF#
E@D0E-OD# alulate?"isount?perentage
###
E@DC$A>># l?!oo&?or"er I0P$E0E@-A-IO@
3455$R2
I thin& I "on%t have to say anything, sine improving the rea"a!ility using meaningful names
an" onstants spea&s for itself# I only want to show you the !efore K after o"e snippets#
Feel the "iHerenes1
efore the improements
2
3
4
5
6
7
8
###
<======================================================================<
C$A>> l?or"2 I0P$E0E@-A-IO@#
<======================================================================<
###
0E-OD al?"p#
IF ms G %;2% A@D t! G %C%#
"p G 4;#
E@DIF#
8/9/2019 Improve Your Abap Code
http://slidepdf.com/reader/full/improve-your-abap-code 16/23
9
2;
22
23
24
25
26
27
28
29
2
3;
IF ms G %;% A@D t! G %-%#
"p G 26#
E@DIF#
IF ms G %;8% A@D t! G %)D%#
"p G 6#
E@DIF#
E@D0E-OD# al?"p
###
E@DC$A>># l?or"2 I0P$E0E@-A-IO@
*fter the improements
2
3
4
5
6
7
8
9
2;
22
23
24
###
<======================================================================<
C$A>> l?!oo&?or"er I0P$E0E@-A-IO@#
<======================================================================<
###
0E-OD alulate?"isount?perentage#
IF month?of?sales G January A@D type?of?!oo& G hil"ren#
"isount?perentage G 4;#
E@DIF#
IF month?of?sales G septem!er A@D type?of?!oo& G travel#
"isount?perentage G 26#
E@DIF#
IF month?of?sales G July A@D type?of?!oo& G we!?"esign#
"isount?perentage G 6#
E@DIF#
8/9/2019 Improve Your Abap Code
http://slidepdf.com/reader/full/improve-your-abap-code 17/23
25
26
27
28
29
2
3;
E@D0E-OD# alulate?"isount?perentage
###
E@DC$A>># l?!oo&?or"er I0P$E0E@-A-IO@
Create a constant, name it after the meaning, and replace the number with it.
OVERVIEW
In the last two !log posts, I have showe" you the !ene/ts of using meaningful names an"
avoi"ing magi num!ers# As I tol" you, it really "oes matter how rea"a!le your o"e is#
In the last few "ays, I was thin&ing a lot a!out the !H2 s, why many=many "evelopers write
their soure o"e with poor rea"a!ility an" transpareny:
• faster deelopment GL o&ay, it an !e faster in the /rst few iterations, !ut then
the "evelopment time inreases largely, when the lines of the soure o"e hits a
ertain num!ers,
• hiding information GL it%s my program1 I "on%t want any!o"y to un"erstan" it1 I
want to &eep that information1 It%s o&, !ut you are going to hi"e that info from
yourself also, sine a year later you won%t un"erstan" neither,
• lainess GL I have no e'planation :
Fin"ing self=e'planatory, meaningful names, an" writing well=organiMe" o"e ta&es time,
that%s true, !ut !elieve me, it%s going to pay oH later#
I have alrea"y mentione" you that you shoul" thin& a!out your o"e as a !oo&, it shoul"
have
8/9/2019 Improve Your Abap Code
http://slidepdf.com/reader/full/improve-your-abap-code 18/23
• a title +for e'ample: program name,
• table of contents +list of metho"s, funtions, su!routines alls, so on,
• detailed chapters +implemente" metho"s, funtions, su!routines, so on#
Don%t forget1 )e are pa""ling with our olleagues an" the others in the same !oat, we form
a team, so "on%t ma&e your olleagues% tas&s more "iult, write rea"a!le an" transparent
o"e that everyone an easily rea" an" un"erstan"#
-o"ay, my goal is to show you the !ene/ts of a well=organiMe" o"e using funtions whether
with funtion mo"ules, su!routines, metho"s, an" so on# I%m going to intro"ue it !ase" on
one of my previous sample soure o"e, on the N0$ proessing e'ample# ave fun1
46OR7$6I1E* O*E
ere is the refatore" +!a&war" version of the N0$ proessing e'ample# -his version
represents the unorganiMe" way of a o"e, the statements are one after the other, we "on%t
get any a""itional information a!out what the o"e is a!out#
0y pro!lem with this o"e that I only an guess
•
why the "eveloper uses I?P$OAD,
• why loops through on an internal ta!le an" uses the statement, CO@CA-E@A-E,
• why alling a transformation,
• why inserting to a "ata!ase ta!le#
But why shoul" I guess these information, if I am a!le to e'press it in a eient way* It%s
har" to interpret, I only an guess, I annot !e sure what the o"e e'atly "oes# It%s a time
onsuming ativity, an" unneessary part of the "evelopment#
)hat shoul" we "o*
2
3
###
>-A-=OF=>E$EC-IO@#
CA$$ F@C-IO@ %I?P$OAD%
8/9/2019 Improve Your Abap Code
http://slidepdf.com/reader/full/improve-your-abap-code 19/23
4
5
6
7
8
9
2;
22
23
24
25
26
27
28
29
2
3;
ENPO-I@
/lename G %C:ustomers?with?attri!ute#'ml%
-AB$E>
"ata?ta! G 'ml?ontent#
$OOP A- 'ml?ontent I@-O 'ml?line#
CO@CA-E@A-E 'ml?ontent?at
'ml?line
I@-O 'ml?ontent?at#
E@D$OOP#
CA$$ -A@>FO0A-IO@ Mustomer?st
>OCE N0$ 'ml?ontent?at
E>$- ustomers G ustomers#
I@>E- Mustomer FO0 -AB$E ustomers#
###
OR7$6I1E* O*E
)hat if I say that the /rst few lines of o"e is responsi!le for uploa"ing a spei/ N0$ /le*
)hy woul"n%t I reate a simple su!routine, an" all it as upload_4ml#
-he ne't few lines, the loop is responsi!le for onverting the reeive" internal ta!le +ontentof the N0$ /le, line !y line into a at string# -hat%s why, we are going to reate another
su!routine, an" all it as conert_to_at_string#
-hen, the CA$$ -A@>FO0A-IO@ statement is responsi!le for parsing a at N0$ /le +N0$
ontent in a simple string format into an **, "ictionary o!Jet, into an internal ta!le#
$et%s reate another su!routine, an" all it as parse_4ml#
8/9/2019 Improve Your Abap Code
http://slidepdf.com/reader/full/improve-your-abap-code 20/23
At last, we perform an insert on the "ata!ase, that means we want to insert the reeive"
ustomers into the "ata!ase# -hat%s why, we are going to reate one more su!routine, an"
all it as insert_into_database#
-his list of su!routine alls is the ta!le of ontents in our !oo& +ABAP program# )hat "o you
thin&* At this stage, you "on%t are a!out how these su!routines are implemente", you only
want to &now the high=level wor&ow of the program, nothing more# -hat%s the power of
organiMe" o"e, it organiMes the o"e into levels +high=level, "etaile"=level to help the
rea"ers un"erstan"ing the o"e# First you rea" the list of function calls +ta!le of
ontents, an" then the implementations +hapters one !y one#
2
3
4
5
6
7
8
9
2;
22
23
###
>-A-=OF=>E$EC-IO@#
PEFO0 uploa"?'ml CA@I@ 'ml?ontent#
PEFO0 onvert?to?at?string >I@ 'ml?ontent
CA@I@ 'ml?ontent?at#
PEFO0 parse?'ml >I@ 'ml?ontent?at
CA@I@ ustomers#
PEFO0 insert?into?"ata!ase >I@ ustomers#
###
$et%s he& the implementations +hapters# ere, I wrappe" all of the logi a!ove in the
setion, 6norganied Code into the separate su!routines !ase" on the "iHerent
responsi!ilities#
-he upload_4ml su!routine alls the 76._6,8(*" funtion mo"ule that returns an internal
ta!le !a& with the ontent of the N0$#
I wrappe" the loop yle into the su!routine, conert_to_at_sting that loops through on
the ontent of the N0$, an" onatenate the atual line into the at string, alle"
4ml_content_at#
8/9/2019 Improve Your Abap Code
http://slidepdf.com/reader/full/improve-your-abap-code 21/23
-hen, I put the simple transformation all into the su!routine, alle" parse_4ml that returns
!a& a ustomers list#
At last, the trivial one, I plae" the insertion into the su!routine, alle"
insert_into_database that atually performs the insertion#
)hat "o you thin&* Atually, the su!routine "e/nitions forms a !lo& that "emarates the
"iHerent responsi!ilities from eah other, an" as a result we get a more transparent an"
rea"a!le o"e than !efore#
2
3
4
5
6
7
8
9
2;
22
23
24
25
26
27
28
29
2
3;
32
###
<K=====================================================================<
FO0 uploa"?'ml CA@I@ 'ml?ontent -YPE string?tt#
<K=====================================================================<
CA$$ F@C-IO@ %I?P$OAD%
ENPO-I@
/lename G %C:ustomers?with?attri!ute#'ml%
-AB$E>
"ata?ta! G 'ml?ontent#
E@DFO0# P$OAD?N0$
<K=====================================================================<
FO0 onvert?to?at?string >I@ 'ml?ontent -YPE string?tt
CA@I@ 'ml?ontent?at -YPE string#
<K=====================================================================<
DA-A 'ml?line $IQE $I@E OF 'ml?ontent#
$OOP A- 'ml?ontent I@-O 'ml?line#
CO@CA-E@A-E 'ml?ontent?at
'ml?line
I@-O 'ml?ontent?at#
E@D$OOP#
E@DFO0# CO@E-?-O?F$A-?>-I@
<K=====================================================================<
FO0 parse?'ml >I@ 'ml?ontent?at -YPE string
8/9/2019 Improve Your Abap Code
http://slidepdf.com/reader/full/improve-your-abap-code 22/23
33
34
35
36
37
38
39
3
4;
42
43
44
45
46
47
48
49
4
CA@I@ ustomers -YPE Mustomer?tt#
<K=====================================================================<
CA$$ -A@>FO0A-IO@ Mustomer?st
>OCE N0$ 'ml?ontent?at
E>$- ustomers G ustomers#
E@DFO0# PA>E?N0$
<K=====================================================================<
FO0 insert?into?"ata!ase >I@ ustomers -YPE Mustomer?tt#
<K=====================================================================<
I@>E- Mustomer FO0 -AB$E ustomers#
E@DFO0# I@>E-?I@-O?DA-ABA>E
###
3455$R2
Funtions are very useful, they help me a lot "uring my "evelopments:
• they an help you to !rea& "own a huge, seemingly unsolva!le pro!lem into small,
interpreta!le tas&s that you an implement muh easier +uploa" =L onvert =L parse=L insert, an" it an help you to thin& in responsi!ilities,
• they an help you to organiMe your o"e in a way that is easy to rea" an" un"erstan"
+you an avoi" unneessary omments with meaningful funtion names,
8/9/2019 Improve Your Abap Code
http://slidepdf.com/reader/full/improve-your-abap-code 23/23
• at last onstruting generi funtions +metho"s, su!routines, funtion mo"ules, an"
so on you an ahieve the !ene/ts of re=usa!ility as well#
O&, this o"e a!ove only a simple e'ample, !ut it "emonstrates the !asis of well=organiMe"
o"e# It%s not a !ig magi, !ut an help you muh more, than you thin&#
@ow, I haven%t overe" any OO priniples, "esign patterns, an" agile metho"s neither,
!eause it%s another fairy tale :# I hope you enJoye" it, stay tune"1