<This documents will explain to the beginners in ABAP that how we can make our code more structured and standardized so that it can become more Interactive. However coding standards are made by SAP Professionals, but the target of this document is to make aware to the persons who is new in Technology .Hope this document will share the things well . >
<
Coding Standards:
While naming the ABAP program, make sure that the Program starts with Z or Y (for all programs created in the Training environment). These are called the Namespaces and if required different namespaces will be provided during the real time project work.
1.2 Program description and modification log
An overall description of the program along with the modification log must be given at the top of all the Program, SAP Script, Print Program, Dialog Program, BDC and all other programs. It should have the following format: *********************************************************
* Author's name :
* Date written :
* Program description :
*
*********************************************************
* Modification # :
* Date of modification:
* Programmer : Name of person modifying code
* Description : description of the modification
*********************************************************
The program includes, type pools must be enclosed between two comment lines as shown below: ****** START OF INCLUDES **************************
INCLUDE ZFUGLSTR.
****** END OF INCLUDES ****************************
All the global data must be defined here. They must be enclosed between two comment lines as follows: ****** START OF DATA DECLARATION ******************
****** END OF DATA DECLARATION ******************** All the items in this section must follow the following sequence: Tables declaration
Type definition
Parameters
Select-options
Constants
Variables
Internal tables
1.4.1 Table definition
Define SAP tables used in the program as with a brief description of the table. Example:
TABLES : EKKO, "Purchasing Document Header
EKPO. "Purchasing Document Line Items
User defined non-elementary types must be prefixed with TY_. Example:
TYPES: BEGIN OF TY_MYTYPE,
NAME (10) TYPE C,
NUMBER TYPE I,
END OF TY_MYTYPE.
SAP R/3 limits the number of characters in parameter name up to 8. Standard
Parameters must be prefixed with P_. Example:
PARAMETER: P_NAME TYPE C. Note: The explanation for the parameter can be given in text element.
SAP R/3 limits the number of characters in select-options name up to 8. Standard
For the SELECTION-SCREEN Parameters use the following.
Select Options SO_
Parameters P_
Radio Buttons R_
Check boxes CH_
Pushbuttons PB_ Example:
SELECT-OPTIONS: SO_NAME FOR ZVEND-NAME. Note: The explanation for the select-options can be given in text element.
Standard
Constants must be prefixed with C_. Example:
CONSTANTS: C_NO_OF_COLS_IN_EXCEL TYPE I VALUE 12.
Standard
The first character of the variable must indicate the scope of the variable.
G - for global variable
L - for local variable The second character of the variable must indicate the type of the variable.
C Character
N Numeric Text
I Integer
P Packed
F Floating Point
H Hex
D Date
T Time
O OLE object
S Structure (any other non-elementary type) The third character must be an underscore. The rest of the characters must indicate the purpose of the variable. Example:
A global variable that holds the document date of financial document can be declared as following:
DATA: GD_DOCUMENT_DATE TYPE D.
1.4.7 Flags
Standard
Declaration of Flags should always start with F_.
Example:
DATA: F_FLAG TYPE C VALUE 'X'.
1.4.8 Ranges
Declaration of Ranges should start with RA_ and Local ranges should be declared as LRA_
1.4.9 Counters
Declaration of Counters / Accumulators should start with CNT_
Example:
DATA: CNT_Counter1 TYPE I.
1.4.10 Field-Groups
Declaration of FIELD-GROUPS should start with FG_
1.4.11 Field-Symbols
Declaration of FIELD-SYMBOLS should start with FS_
Example:
DATA: <FS_T> TYPE ANY.
1.4.12 Type Reference Declaration.
All objects with the declaration TYPE REF TO should start with LO_
1.4.13 Internal tables
Standard
Internal tables must be named with the prefix GT_ for Global tables and LT_ for Local Tables. Example:
DATA: GT_CUSTOMER LIKE ZCUST OCCURS 0 WITH HEADER LINE.
It is always better to use a Work Area for a Table instead of creating a table with a Header Line and using that.
Work areas can be created globally as well as we can use Local Work Areas. Please make sure that there is proper and valid clearing of the Work Areas before they are used in the program.
Global Work Areas should always start with WA_
Local Work Areas should always start with LWA_
Example:
DATA: WA_CUSTOMER TYPE TYPE_CUSTOMER_TABLE.
1.6 Main program logic
The main program logic must be enclosed between the following comment lines:
1. All ABAP programs must be structured. Forms must be used.
****** START OF MAIN LOGIC ************************
****** END OF MAIN LOGIC **************************
Proper comments are to be written to explain the logic used in the code and the flow of the program. The data declarations should also be explained with the elements Description.
The pretty printer option in the ABAP/4 workbench editor's menu option must be used to indent the code properly. Also note Pretty Printer should always be used only when the code is developed.
Please refrain from using Pretty Printer on the Objects that are already created, which are being modified by you.
Line width should not exceed 72 Characters.
1.8 Attributes
Proper Program Attributes should be maintained even if it is a test program. SE38 -> GOTO -> Attributes. Or from the attributes screen that comes at the time of program Creation.
1.9 Proper Creation and usages of Include files.
XXX_TOP Include - Used for all the declarations
XXX_F01 Include - Used for the Subroutines and the Functions
XXX_D01 Include - Used for writing the Selection Screen parameters
XXX_I01 Include - Used for the PAI Module in Dialog Program
XXX_O01 Include - Used for the PBO Module in the Dialog Program.
In the above includes XXX is the Program name, that is calling the Includes.
| |
DEFING ABAP DATA OBJECTS | |
TABLES AND DDIC STRUCTURES | |
| |
INTERNAL TABLES AND PROGRAM STRUCTURES | |
| |
DATA VARIABLES | |
| |
CONSTANTS | |
| |
INCLUDES | |
| |
SELECTION-SCREEN : Parameters, Select-Options, ... | |
| |
MAIN PROGRAM | |
| |
PAGE HEADER, PAGE FOOTER | |
| |
INTERACTIVE LIST EVENTS | |
| |
FORMS | |
|
|
***The Report Program Events should always be called in the following order.
§ INITIALIZATION
§ AT SELECTION-SCREEN OUTPUT
§ AT SELECTION SCREEN ON VALUE-REQUEST
§ AT SELECTION SCREEN ON HELP-REQUEST
§ AT SELECTION SCREEN ON <parameter>
§ AT SELECTION-SCREEN
§ START-OF-SELECTION
§ GET <node>
§ END-OF-SELECTION
§ TOP-OF-PAGE
§ TOP-OF-PAGE DURING LINE-SELECTION
§ END-OF-PAGE
§ AT LINE-SELECTION
§ AT USER-COMMAND
§ FORM
§ ENDFORM
***Performance tips specifically with Internal Tables:
Ø It must be Cleared / Refreshed wherever necessary.
Ø APPEND to be used instead of COLLECT.
Ø Usage of Memory ID should be minimal & freed immediately after use.
Ø CLEAR the Header Line / Work Area after the APPEND statement.
Ø CLEAR Header Line after DELETE statement.
2. Performance Measures.
2.1 Use Proper Text Elements instead of Hard-coding the text.
Example:
WRONG
Write: 'This is wrong'.
CORRECT
Write: text-t02. "Text-t02 contains 'This is Wrong'
2.2 Use CHECK Statements at all required places as we can avoid unnecessary processing of code.
Example:
SELECT name age FROM EMPL INTO TABLE gt_main_table
WHERE age = 25.
Check gt_main_table[] is not initial.
Other select queries.......
2.3 No Select Statement within a LOOP Statement.
Example:
LOOP AT gt_main_table INTO wa_main_table.
SELECT ...
ENDLOOP.
2.4 Avoid calling of Function Modules within a LOOP Statement.
Example:
LOOP AT gt_main_table INTO wa_main_table.
CALL FUNCTION 'GENERATE_NUMBERS'.
.........
ENDLOOP.
2.5 Use the Keyword SPACE instead of ' '.
Example:
IF CC_text = SPACE. " Instead of ' '.
ENDIF.
2.6 Every SELECT, READ, DELETE, INSERT, APPEND, MODIFY Statement should have a SY-SUBRC Check.
Example:
SELECT name age FROM EMPL INTO TABLE gt_main_table
WHERE age = 25.
IF sy-subrc NE 0.
WRITE text-t04. "t04 - Error.
ENDIF.
2.7 Even if SY-SUBRC check is not required there should be an empty SY-SUBRC Check.
Example:
SELECT name age FROM EMPL INTO TABLE gt_main_table
WHERE age = 25.
IF sy-subrc NE 0.
"Empty Sy-subrc Check.
ENDIF.
2.8 Tables should be SORTED before a READ Statement. READ Statements are better performed when done on a Table with the BINARY SEARCH option used.
Example:
Sort gt_main_table by Name.
READ TABLE gt_main_table INTO wa_main_table WITH KEY name = 'TEST' BINARY SEARCH.
IF sy-subrc NE 0.
WRITE text-t04. "t04 - Error.
ENDIF.
2.9 The ERROR, WARNING, INFORMATION MESSAGES should NOT be Hard Coded.
Example:
WRONG:
MESSAGE 'This is correct' TYPE E.
CORRECT
MESSAGE text-t05 TYPE E.
Note: Other people's articles