22 ABAP Tips for Better Programming

ABAP tips and tricks for better programming. ABAP as such is a very simple programming language and it’s very easy to learn but there are certain things which determine a good programmer from an average programmer.

  1. Use the CHECK command instead of an IF statement whenever possible.  This reduces processing time and improves readability.
  2. Use the ON CHANGE OF command instead of developing code to compare current and previous values of a data field.
  3. Reduce expense by using SELECT SINGLE * FROM instead of SELECT * FROM for retrieving a table entry when the full key is known. use buffered data selecting technique rather than fetching the data from the database directly.
  4. Use the EXIT command to leave a loop structure as soon as a specified condition is met.  This reduces needless loop processing.
  5. Use APPEND instead of COLLECT to add entries to an internal table whenever possible.  The former command has less overhead.
  6. Use the AT command within loop processing whenever possible.
  7. Initialization Event:
    This event is executed before the output of the selection screen.  The INITIALIZATION section is only performed when the ABAP is started directly and is not executed if the ABAP is called or executed by another ABAP.  This event is also ignored in a batch environment.  It is frequently used to calculate special defaults for the selection screen.
  8. Table Loop:
    Whenever looping on a table, always CLEAR the table header or fill it with a generic key before the LOOP statement.
  9. Structured And Modular Programming Techniques :
    The use of structured and modular programming techniques, along with extensive but reasonable comments, greatly enhances the readability and maintainability of a program.
  10. Make use of CASE structures in place of extensive nested IF’
  11. When one uses the MOVE statement, try to keep the destination operands as the same data type as the ‘from’ operands.
  12. Use the FREE <table> command once the program has completed using the table. If the program ends on completion of the table processing, this is obviously not required even though SAP has a robust garbage collection in place.
  13. When defining DATA statements only define variable required throughout the program as global (i.e. at the top of the program). If variables are only required for processing within a FORM/ METHODS, then define the variable within the FORM/METHODS itself.
  14. When using the SORT statement, always specify the sort key. Try to keep the key as short as possible.
  15. When processing an internal table using the LOOP instead of an SQL select statement. When using the LOOP statement use the LOOP…AT…WHERE instead of a LOOP…AT…CHECK and if possible use parallels cursors inside loops.
  16. When the LOOP…AT…WHERE is used ensure the condition variables have been defined with the same data type.
  17. Use the BINARY SEARCH, when performing a direct read on an internal table.
  18. Use a BINARY SEARCH read and MODIFY instead of COLLECT if the internal table is large.
  19. Use the CASE statement instead of a nested IF.
  20. lastly… Beautify the program, use the pretty printer.
  21. Some common errors and how to fix the errors related to SAP variants.
    • Did some one create a protected variant?  Run Program RSVARENT, this will allow you to cancel the protected variant.
    • Incase if you made some changes like the field length, or changed the type, run the program RSVARFIT, it will allow you to adjust variants to modified selection criteria.
    • Have any obsolete variants?  run the program RSVARDOC
    • What to find out were a particular variant is used in SAP? Run the program RSVARVAR. It will list the programs where ever the variant name is used.
  22. Parameter ID is a three character ID that represents specific SAP fields.  For example, BUK is associated with Company code, GJR is associated with Fiscal Year and so..on. This saves a lot of time during transaction entries and would avoid potential data entry errors.

    How to find out the Parameter IDs in SAP?  on Any screen field, Press F1 and click on Technical Information.

    You can either go to transaction SU3 or follow the path System ->user profile -> own data -> parameters and then give the parameter name and the parameter value to maintain the values.