ABAP Programming Guidelines

Programming Guidelines


Programming guidelines are indispensable in large software development projects because they are used to standardize source code structures and development objects. and therefore improve both the readability and comprehensibility of developments in ABAP.

1. Good Readability of Custom Programs
Don't use short names for variables like i1, i2, a, b, and so on, that could confuse at times. 
Use clear and understandable names for the variables.

2.Easy Orientation in External Programs
For example, if you generally adhere to the layer model regarding functions, external developers will find it much easier to understand the program structure.



3.Simple Modification and Maintenance of Programs
If, for instance, you use factory methods and interfaces to generate classes, it is very easy to implement completely new customer-specific classes, which can be used by the remaining program code as well because they use the same interface .

4. Good Quality due to the Avoidance of Errors
A standardized, tried-and-tested software development procedure cannot prevent you from making severe errors in your concept. but it can help you avoid small errors that are difficult to find at a later
stage.

Best Technique in ABAP - Using Hashed Internal Table

Hashed Internal table faster access

The source tables ITAB1 and ITAB2 are standard tables. It is assumed that ITAB1 takes more entries than ITAB2. Otherwise, the table with more entries must be computed with "DESCRIBE TABLE ... LINES ...".
Since both tables shall represent sets, it is assumed that their entries are unique with respect to component K.

The algorithm works with a temporary table with unique key K. The table is a copy of ITAB1 and is used to locate the entries being also contained in ITAB2. The matching entries are copied to ITAB3.

The left-hand and right-hand side differ only by the kind of the temporary table being used. For a hashed table, the READ statement in the LOOP is faster than for the sorted table.


Using a sorted table 



STAB1 = ITAB1.
REFRESH ITAB3.
LOOP AT ITAB2 ASSIGNING <WA>.
  READ TABLE STAB1 FROM <WA>
                   TRANSPORTING NO FIELDS.
  IF SY-SUBRC = 0.
    APPEND <WA> TO ITAB3.
  ENDIF.
ENDLOOP.

FREE STAB1.

Using a hashed table


HTAB1 = ITAB1.
REFRESH ITAB3.
LOOP AT ITAB2 ASSIGNING <WA>.
  READ TABLE HTAB1 FROM <WA>
                   TRANSPORTING NO FIELDS.
  IF SY-SUBRC = 0.
    APPEND <WA> TO ITAB3.
  ENDIF.
ENDLOOP.

FREE HTAB1.


For more Tutorials, visit ABAP Tutorials, Tips & Tricks and Certification Questions

Best Technique to use Nested Loop in ABAP Programming


Nested Loops in ABAP


If ITAB1 has n1 entries and ITAB2 has n2 entries, the time needed for the nested loop with the straightforward algorithm is O(n1 * n2), whereas the parallel cursor approach takes only O(n1 + n2) time.

The above parallel cursor algorithm assumes that ITAB2 contains only entries also contained in ITAB1.  If this assumption does not hold, the parallel cursor algorithm gets slightly more complicated, but its performance characteristics remain the same.

Straightforward nested loop 


LOOP AT ITAB1 INTO WA1.
  LOOP AT ITAB2 INTO WA2
                WHERE K = WA1-K.
    " ...
  ENDLOOP.

ENDLOOP.

More sophisticated loop: parallel cursors 

I = 1.
LOOP AT ITAB1 INTO WA1.
  LOOP AT ITAB2 INTO WA2 FROM I.
    IF WA2-K <> WA1-K.
      I = SY-TABIX.
      EXIT.
    ENDIF.
    " ...
  ENDLOOP.
ENDLOOP.

For more Tutorials, visit ABAP Tutorials, Tips & Tricks and Certification Questions

Best Techniques in ABAP Programming - Internal table appending


Joining Two Internal Tables - Best Technique


If ITAB1 has n1 entries and ITAB2 has n2 entries, the time needed for joining ITAB1 and ITAB2 with the straightforward algorithm is O( n1 * log2( n2 ) ), whereas the parallel cursor approach takes only O( n1 + n2 ) time. 

The parallel cursor algorithm assumes that ITAB2 is a secondary table containing only entries also contained in primary table ITAB1. 

If this assumption does not hold, the parallel cursor algorithm gets slightly more complicated, but its performance characteristics remain the same. 

Normal Technique used by ABAPers  

LOOP AT ITAB1 INTO WA1.
  READ TABLE ITAB2 INTO WA2
             WITH KEY K = WA1-K BINARY SEARCH.
  IF SY-SUBRC = 0.
    " ...
  ENDIF.
ENDLOOP.

Parallel Cursor Technique

DATA: I TYPE I.

I = 1.
LOOP AT ITAB1 INTO WA1.
  do.
    READ TABLE ITAB2 INTO WA2 INDEX I.
    IF SY-SUBRC <> 0. EXIT. ENDIF.
    IF WA2-K < WA1-K.
      ADD 1 TO I.
    ELSEIF WA2-K = WA1-K.
      " ...
      ADD 1 TO I.
      EXIT.
    ELSE.
      EXIT.
    endif.
  enddo.
  if sy-subrc <> 0. exit. endif.
ENDLOOP.


For more Tutorials, visit ABAP Tutorials, Tips & Tricks and Certification Questions

Best Practices in ABAP Programming - SELECT Query



Tips# 1:

Always specify your conditions in the Where-clause instead of
checking them yourself with check-statements.
The database system can then use an index (if possible) and the
network load is considerably less.


Example:

Don'ts 

Select Query with CHECK statement will reduce the performance of the query.

SELECT * FROM SBOOK INTO SBOOK_WA.
  CHECK: SBOOK_WA-CARRID = 'LH' AND
         SBOOK_WA-CONNID = '0400'.
ENDSELECT.

Do's

Use WHERE condition Instead.

SELECT * FROM SBOOK INTO SBOOK_WA
  WHERE CARRID = 'LH' AND
        CONNID = '0400'.

ENDSELECT.


Tips# 2:

If you are interested if there exists at least one row of a database  table or view with a certain condition, use the Select ... Up To 1 Rows statement instead of a Select-Endselect-loop with an Exit.

If all primary key fields are supplied in the Where condition you can even use Select Single.
Select Single requires one communication with the database system, whereas Select-Endselect needs two.

Example:

Don'ts

Select Query loop with EXIT statement is not good practice.

SELECT * FROM SBOOK INTO SBOOK_WA
    WHERE CARRID = 'LH'.
  EXIT.
ENDSELECT.

Do's

Use Upto 1 row instead of EXIT statement or use SELECT SINGLE statement to increase the performance.

SELECT * FROM SBOOK INTO SBOOK_WA
  UP TO 1 ROWS
  WHERE CARRID = 'LH'.

ENDSELECT.


SELECT SINGLE * FROM SBOOK INTO SBOOK_WA
  WHERE CARRID = 'LH'.

ENDSELECT.


Tips# 3

If you want to find the maximum, minimum, sum and average value or the count of a database column, use a select list with aggregate functions instead of computing the aggregates yourself.

Network load is considerably less.


Don'ts  

Calculation of a field is not a good practice to be used inside a SELECT loop Statement.

DATA: MAX_MSGNR type t100-msgnr.
MAX_MSGNR = '000'.
SELECT * FROM T100 INTO T100_WA
  WHERE SPRSL = 'D' AND
        ARBGB = '00'.
  CHECK: T100_WA-MSGNR > MAX_MSGNR.
  MAX_MSGNR = T100_WA-MSGNR.
ENDSELECT.


Do's

Use aggregate function and reduce the complexity of SELECT loop.

DATA: MAX_MSGNR type t100-msgnr.
SELECT MAX( MSGNR ) FROM T100 INTO max_msgnr
  WHERE SPRSL = 'D' AND

        ARBGB = '00'.

Tips# 4

Use a select list or a view instead of Select * , if you are only interested in specific columns of the table. Network load is considerably less. 

Example

Don'ts

Using * to select all the fields instead of specific fields reduces the performance during Runtime.

SELECT * FROM DD01L INTO DD01L_WA
  WHERE DOMNAME LIKE 'CHAR%'
        AND AS4LOCAL = 'A'.
ENDSELECT.  
Do's

Select only the specific field needed for processing the program.

SELECT DOMNAME FROM DD01L
  INTO DD01L_WA-DOMNAME
  WHERE DOMNAME LIKE 'CHAR%'
        AND AS4LOCAL = 'A'.
ENDSELECT.


Tips# 5:

Whenever possible, use column updates instead of single-row updates to update your database tables.
Network load is considerably less.


Example

Don'ts


SELECT * FROM SFLIGHT INTO SFLIGHT_WA.
  SFLIGHT_WA-SEATSOCC =
    SFLIGHT_WA-SEATSOCC - 1.
  UPDATE SFLIGHT FROM SFLIGHT_WA.

ENDSELECT.

Do's

UPDATE SFLIGHT

       SET SEATSOCC = SEATSOCC - 1.