ABAP’s – ‘Tips’ and ‘Traps’ alongwith New syntax
Powered by the continuous thrust on self growth, I ventured upon using the new programming syntax in ABAP. Along the way I learnt some tips and tricks and discovered few shortcomings as well. Through this post I would like to present a gist of this journey based on my experiments. Also, thanks to Jeffrey Towell for his wonderful blog here.
Disclaimer: As already stated, all these are based on my experiences and may have some gaps which I would love to learn from feedback and your experiences.
Tip # 1. Frequently use sorted tables within table expression as it triggers an implicit Binary Search since we don’t have any provision to specify ‘BINARY SEARCH‘ clause in table expression.
DATA : lt_mseg TYPE SORTED TABLE OF mseg WITH NON-UNIQUE KEY grund. DATA : ls_mseg TYPE mseg. " Below will trigger implicit Binary Search ls_mseg = lt_mseg[ grund = '<Some value>' ].
Tip # 2. Use of Value operator only works with Data types accessible in current scope.
Sandra Rossi : Thank you for correcting me.
" Class 1 - Method 1 DATA : ls_val TYPE bwart_range. " ts_val - Public type in Class 1 ls_val = VALUE ts_val( low = 'ABC' ). " Class 2 - Method 1 DATA : ls_val TYPE bwart_range. " ts_val - Public type in Class 1 ls_val = VALUE Class1=>ts_val( low = 'ABC' ). " -------------------------------------------------------- " Class 1 - Method 1 DATA : ls_val TYPE bwart_range. " ts_val - Private type in Class 1 ls_val = VALUE ts_val( low = 'ABC' ). " Class 2 - Method 1 DATA : ls_val TYPE bwart_range. " ts_val - Private type in Class 1 so gives Syntax Error ls_val = VALUE Class1=>ts_val( low = 'ABC' ).
Tip # 3. Define sorted tables carefully. There is difference between both syntaxes –
a. Data: <Itab> TYPE SORTED TABLE OF < > with <Key Attributes> <Field 1> , <Field 2 >.
b. Data: <Itab> TYPE SORTED TABLE OF < > with <Key Attributes> <Field 1> <Field 2 >. ( Generally we use this one )
Matthew Billingham Thank you for giving a good example.
DATA : lt_po_detail_sort TYPE SORTED TABLE OF ekpo WITH NON-UNIQUE KEY ebeln, ebelp. " Same as DATA : lt_po_detail_sort TYPE SORTED TABLE OF ekpo WITH NON-UNIQUE KEY ebeln. DATA ebelp. " Declares a single character variable
Tip # 4. < Use the Corresponding operator as below
<Structure1 – Data type 1 > = CORRESPONDING <Data type 1> ( BASE ( < Structure 2 – Data type 1 > ) < Structure 3 – Data type 2 > )
DATA: ls_mara_proc TYPE zmara, ls_mara TYPE zmara, ls_admin_data TYPE zs_admindata. " Combine the custom material header " fields and admin data " Point to note is component fields in " structure zs_admindata are also in zmara ls_mara_proc = CORRESPONDING zmara( BASE ( ls_mara ) ls_admin_data ).
Tip # 5. We can use Table Expression within For Loop as well. Something like
<Itab 1> = Value <Table Type>( FOR <Work Area> IN < Itab 2 with data> ( <field1> = <Itab 3> [ < search clause > ]-<field 1>
<field2> = < Work Area>-<field 2> ) )
DATA: lt_quality TYPE ztt_quality, lt_quality_db TYPE ztt_quality, lt_mat_plant TYPE SORTED TABLE OF zs_mat_plant WITH NON-UNIQUE KEY matnr. " Map the selected materials and plants lt_quality = VALUE ztt_quality( FOR ls_quality IN lt_quality_db ( matnr = ls_quality-matnr werks = lt_mat_plant[ matnr = ls_quality-matnr ]-werks ) ).
Tip # 6. We can use Corresponding With Base, Mapping, Except and with Value operator. It is very handy when we need to combine data from multiple structures in a single structure.
Examples from standard documentation.
Tip # 7. With Assign statement in Table expression where use field symbol, we need not check the exception cx_sy_itab_line_not_found instead we should check the value of sy-subrc which is set to indicate read failure.
Tip # 8. We can directly unassign field symbols without checking if they are assigned but we cannot directly clear field symbols without checking if they are assigned i.e.
UNASSIGN < Field symbol > – Works
CLEAR < Field symbol > – Does not work (dumps if field symbol is not assigned)
Tip # 9. Whenever we have situations to apply control breaks etc. we should go for
‘Group By‘ clause instead. It’s more readable , easy to maintain and does not come with
hassle of fields getting asteriked. Also, it does not cause quadrtic loops.
LOOP AT ct_all_mat ASSIGNING FIELD-SYMBOL(<fs_all_mat>) GROUP BY ( field1 = <fs_all_mat>-field1 ) ASCENDING ASSIGNING FIELD-SYMBOL(<fs_all_mat_grp_tab>). " Prepare the sequence number as a running number " irrespective of field1 LOOP AT GROUP <fs_all_mat_grp_tab> ASSIGNING FIELD-SYMBOL(<fs_all_mat_grp_rec>). " Processing for all item in a Group ENDLOOP. " Processing once for each matching group. ENDLOOP.
I will keep adding as I learn.
Perhaps you should have a look at the already existing quick reference:
It might reduce your workload a bit… ?
Yes, Dominik I have already referenced the wonderful material by Jefferey while learning new syntax myself and I will definitely add this reference here.
My point of compiling this list was to collect some practical points which one can keep in mind before starting development as a best programming or hygienic programming practices, if I may say.
Thanks for the feedback.
Tip # 2. Use of Value operator only works with Data Dictionary types.
Some tips are not about the “new programming syntax” (you mean the constructor operators in ABAP 7.40) : Tips # 3 and 9 used to exist for 20 years.
In tip # 7, it’s not obvious what you mean with “database type”.
Generally speaking, you should better provide the ABAP code, otherwise people will get confused.
Thanks a lot for the feedback. I have tried incorporating all.
Also, I was not sure about old tip #7 so removed it for now.
It looks smarter now. Tip # 7 should be closer to Tip # 1 because they both concern Table Expressions (in Tip # 1 you could provide a code with the OPTIONAL word so that it handles "line not found", and point to Tip # 7 (that you shoud renumber # 2) to explain how to hande "line not found" by either using the DEFAULT or OPTIONAL words, or using ASSIGN, or handling cx_sy_itab_line_not_found.
No-one should be using standard tables, sorting them and then reading them with BINARY SEARCH any more. I appreciate that SORTED tables have only been available for 18 years…
And what about HASHED tables?
Er…. right… Colon and comma confusion has certainly been around since I began ABAP, with 30D.
Tip 9: is again nothing new to 7.4. It’s been around for well over twenty years.
I think you should follow what Sandra Rossi said. Provide actual examples, and perhaps also maybe change your blog title
“Tips and traps I’ve found while learning ABAP”.
Thanks for the feedback. I have updated the blog title based on your apt suggestion and few points as well.
You might want to update Tip 1 as well since suggestion to use sorted tables "always" is misleading. We use the type that is most suitable for a specific task. As Matthew mentioned, hashed tables exist as well and there are also secondary indexes for internal tables (see this blog).
If I may suggest, instead of posting multiple tips, most of which end up being corrected, why not take a single subject and explore it in depth? E.g. how did you come up with "always" recommendation? Did you try using different table types in different situations? Did you analyze performance? Did you find any SCN blogs or documentation on the subject? What were they missing or left unclear?
This is, of course, just an example and table types have been covered ad nauseum already but a more thorough investigation usually has more interesting findings.
Thank you for the feedback. Sincereley appreciated.
Through this blog I wanted to bring out a quick perspective for countering commonly faced development problems. I could have very well taken this as a research topic and definitely it would have uncovered some learnings for me (and community).
Hi, about the tip #5 you dont need to use 2 times the type ZTT_QUALITY. You can use one of the 2 shorter variants :
either use the # to refer to the definition of ztt_quality
either define the variable inline :
Thanks for the feedback.
I agree with you. Both options suggested by you will definitely work. It's just that I avoid declaring with '#' because it becomes bit difficult to maintain as multiple clicks are needed to identify the associated data types.
Useful tips. Thanks Ankit!
Thanks Ramin for the feedback.
Hi Jeffrey Towell ,
Thanks a lot, your blog served an inspiration to me.
Thanks and Regards.