Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
Ankit_Maskara
Product and Topic Expert
Product and Topic Expert
37,573
Hi Community,

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.towell2  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.

 

 
15 Comments