Sap Abap Naming Conventions
Sap Abap Naming Conventions
C Character 1 Space
N Numeric text 1 '00...0'
D Date YYYYMMDD 8 '00000000'
T Time HHMMSS 6 '000000'
X Byte (heXadecimal) 1 X'00'
I Integer 4 0
P Packed number 8 0
F Floating point number 8 '0.0'
STRING String variable empty string
XSTRING Byte sequence (X string) variable empty X string
The system field SY-DBCNT contains the number of lines read so far ecah time the
SELECT statement is executed. After ENDSELECT, SY-DBCNT contains the total
number of records read.
Dictionary Objects
Domain Z*
Data Element Z*
Lock Object EZ*
Search Help ZSH*
Structure ZS*
Table Z* or ZT*
Table Type Z* or ZTY*
Table Indeces Z## 3 character unique name
Type Group ZTG*
View ZV*
R - Report
D - Data Load
I - Include
X - Interface
etc.
Application Log Z*
Area Menu Z*
Batch Input Session Z*
Change Document Z*
Package Z*
Dialog Module Z*
CMOD Enhancement Z*
Project
Form Pool SAPFZ*
For Example
FZ*TOP - Data declarations
FZ*F## - Subroutines
Function Groups Z*
Function Modules Z_*
Gui Status Z*
Include ZI*
Memory ID ZM*
Message Class Z*
Messages Number numeric 3 digits 1-999
Module Pool SAPMZ*
For Example
MZ*TOP - Data declarations
MZ*O## - PBO modules
MZ*I## - PAI modules
MZ*F## - Subroutines
Number Range ZNR*
Program Ztype* where type identifies the program
function
Screen 4 characters
GUI Title Z*
Transaction Code Z*
Naming Conventions
Naming Conventions lend consistency and readabilty to your code. This in turn improves the overall maintainability of
code. When naming your objects and variables, try to avoid arcane acronyms. Spell it out where possible.
Refer to the table below for accepted industry standard naming conventions for objects and variables within a program.
Interface Parameters
Subroutine Formal Parameters Convention Example
Using u_* or p_*
Changing c_* or p_*
Raising r_* or p_*
Functions/Methods
FUNCTIONS
Use the built in documentation features found in SE37 to document the purpose of the function modules and the usage
of the Interface parameters.
The use of naming standards for interface parameters is not a common practice, although it should be so SAPdomain
recommends the following:
METHODS
For Methods Use the Following naming standard for the signature of the method.
Importing: IM_*
Exporting: EX_*
Changing : CH_*
Returning: RE_*
Raising: exception (no std)
Exceptions: exception_class (no std)
Events
List Processing Events are executed in a predefined order according to the rules of the ABAP interpreter and therefore
the order in which they appear in an ABAP program is not important. However there is a standard practice among
programmers (for readability) to order events according to the order of execution.
Error Handling
This guideline is the single most overlooked coding practice by developers but yet one of the most
critical techniques available that ensures fail safe programming that avoids potential failure points.
Never make an assumption that a piece of code will always work. By putting in proper subrc checking,
your program is assured that it can at least handle erroneous situations. Fail safe programming is
critical to quality applications.
The programmer should always, at minimum, put in a generic 'catch-all' check that will catch any failed
condition. More specific checks can always be added.
Proper message issuing and exception processing makes for a program that is easy to support and
maintain. It is a key component of quality programming. It is also a tell tale sign of the skill, experience,
and attitude of a developer towards quality programming.
Snippet:
IF sy-subrc = 0.
code.
Else.
* no code
ENDIF.
OR
IF sy-subrc = 0.
code for successful operation only
ENDIF.
IF sy-subrc = 0.
code.
Else.
code.
ENDIF.
Or
IF sy-subrc <> 0.
code.
ENDIF.
OR
When no action is required after a non zero code, then
simply add a comment. This at least indicates that the programmer gave
some thought to the outcome of a failed condition which in some cases is
OK to continue processing.
Sample comment:
* non zero return code is OK. Continue processing
“MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2, sy-msgv3, sy-
msgv4”.
This statement assumes that SY variables have been filled with meaningful values by the function itself.
However, often times, this is not the case.
The generic message should only be used in the case where the function and all nested functions that
are called use the Message...Raising format consistently throughout the function chain. The
Message...Raising format sets the sy-message and other sy variables. Only then, can you be assured
that a generic message statement can be used to present a proper error message. However, since this
situation is most likely not the case, a good developer will handle the message themselves and not
count on the generic form to provide a meaningful message relevant to the situation.
Snippet:
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty
NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
CALL FUNCTION 'Z_MATERIAL_VIEW'
EXPORTING
im_full_details = 'X'
im_t_material = t_materials
EXCEPTIONS
invalid_input = 1
invalid_material = 2
no_materials = 3
OTHERS = 4.
CASE sy-subrc.
WHEN 1.
MESSAGE e999 WITH 'Invalid Input'(101).
WHEN 2.
MESSAGE e999 WITH 'Invalid Material'(102).
WHEN 3.
MESSAGE e999 WITH 'No Materials'(103).
WHEN 4.
MESSAGE e999 WITH 'Other Reasons'(104).
ENDCASE.
Message Processing
Guideline: Use the correct message type A,E,X,W,I,S to determine the expected outcome
Explanation: When issuing error messages, the flow of the program may take different paths based on the message
type and the context in which the message is issued.
SAP online Help provides a good explanantion of the message types and the program reaction
depending where in a processing block the message is issued.
For Example
- An 'S' message reacts differently in an AT event than it does when issued in a FORM.
- An 'E' Message in the At Selection Screen Event will require the user to re-enter the input whereas a
W message is simply a warning and the user only needs to press ENTER to bypass the warning
condition so that program processing can resume.
Suggestion: Messages in all events need to be evaluated to determine if there is a reason to stop the
program with an E type message or have it continue processing.
Executable programs should always allow the user the option to exit or correct an error situation, for
example in a selection Screen. That’s why E and W messages need to be used with knowledge of the
outcome. TEST the situation thoroughly if you are unsure and then TEST again even if you are sure.
In Background Jobs, when a program is executed in the background, a message issued of type ‘E’, ‘A’
or ‘X’ will cancel the execution of the job. The job log is a handy feature to determine status of a job.
Messages issued in background show up in the job log so be sure to provide meaningful messages that
will easily identify the point of failure and course of action required.
If an online program is executed in background, the messaging can be bypassed by first checking to
see if it is running in background by evaluating the sy-batch variable. If the program is running in
background, then do not issue error messages that would typically be used to interact with the user.
For Online Selection Screen type programs - use the Write statement to issue the message to output
and then issue a type S message and then a Leave List-Processing statement should follow this. This
returns the user back to the selection screen.
Snippet:
under construction
Functions
“MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2, sy-msgv3, sy-
msgv4”.
This statement assumes that SY variables have been filled with meaningful values by the function itself.
However, often times, this is not the case.
The generic message should only be used in the case where the function and all nested functions that
are called use the Message...Raising format consistently throughout the function chain. The
Message...Raising format sets the sy-message and other sy variables. Only then, can you be assured
that a generic message statement can be used to present a proper error message. However, since this
situation is most likely not the case, a good developer will handle the message themselves and not
count on the generic form to provide a meaningful message relevant to the situation.
Snippet:
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty
NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
CASE sy-subrc.
WHEN 1.
MESSAGE e999 WITH 'Invalid Input'(101).
WHEN 2.
MESSAGE e999 WITH 'Invalid Material'(102).
WHEN 3.
MESSAGE e999 WITH 'No Materials'(103).
WHEN 4.
MESSAGE e999 WITH 'Other Reasons'(104).
ENDCASE.
Message Types - A E X I W S
Message Processing
Guideline: Use the correct message type A,E,X,W,I,S to determine the expected outcome
Explanation: When issuing error messages, the flow of the program may take different paths based on the message
type and the context in which the message is issued.
SAP online Help provides a good explanantion of the message types and the program reaction
depending where in a processing block the message is issued.
For Example
- An 'S' message reacts differently in an AT event than it does when issued in a FORM.
- An 'E' Message in the At Selection Screen Event will require the user to re-enter the input whereas a
W message is simply a warning and the user only needs to press ENTER to bypass the warning
condition so that program processing can resume.
Suggestion: Messages in all events need to be evaluated to determine if there is a reason to stop the
program with an E type message or have it continue processing.
Executable programs should always allow the user the option to exit or correct an error situation, for
example in a selection Screen. That’s why E and W messages need to be used with knowledge of the
outcome. TEST the situation thoroughly if you are unsure and then TEST again even if you are sure.
In Background Jobs, when a program is executed in the background, a message issued of type ‘E’, ‘A’
or ‘X’ will cancel the execution of the job. The job log is a handy feature to determine status of a job.
Messages issued in background show up in the job log so be sure to provide meaningful messages that
will easily identify the point of failure and course of action required.
If an online program is executed in background, the messaging can be bypassed by first checking to
see if it is running in background by evaluating the sy-batch variable. If the program is running in
background, then do not issue error messages that would typically be used to interact with the user.
For Online Selection Screen type programs - use the Write statement to issue the message to output
and then issue a type S message and then a Leave List-Processing statement should follow this. This
returns the user back to the selection screen.
This guideline is the single most overlooked coding practice by developers but yet one of the most
critical techniques available that ensures fail safe programming that avoids potential failure points.
Never make an assumption that a piece of code will always work. By putting in proper subrc checking,
your program is assured that it can at least handle erroneous situations. Fail safe programming is
critical to quality applications.
The programmer should always, at minimum, put in a generic 'catch-all' check that will catch any failed
condition. More specific checks can always be added.
Proper message issuing and exception processing makes for a program that is easy to support and
maintain. It is a key component of quality programming. It is also a tell tale sign of the skill, experience,
and attitude of a developer towards quality programming.
Snippet:
IF sy-subrc = 0.
code.
Else.
* no code
ENDIF.
OR
IF sy-subrc = 0.
code for successful operation only
ENDIF.
IF sy-subrc = 0.
code.
Else.
code.
ENDIF.
Or
IF sy-subrc <> 0.
code.
ENDIF.
OR
When no action is required after a non zero code, then
simply add a comment. This at least indicates that the programmer gave
some thought to the outcome of a failed condition which in some cases is
OK to continue processing.
Sample comment:
* non zero return code is OK. Continue processing
Performance
Performance desc
Internal Tables should always be sorted before using DELETE ADJACENT DUPLICATES. Delete
Adjacent Duplicates should always be explicit by using the COMPARING Clause, even if there is only
one field in the itab.
When sorting internal tables, always use explicit sort keys i.e. use "SORT BY Key1...n", never just
"SORT" on it's own. Not following this practice could lead to unexpected situations if the structure of the
table changes.
Keeping the "SORT itab BY" statement as close as possible to the READ itab with KEY...Binary Search
is a good coding practice.
Snippet:
SORT T_DOCUMENT.
DELETE ADJACENT DUPLICATES FROM T_DOCUMENT.
Constants
Guideline: Use Keyword CONSTANT for variables that do not change rather than using the DATA Keyword
Explanation: Defining a variable as a constant prevents anyone from changing the value of the constant especially
when the program gets maintained in the future. This guarantees that the value can never be changed
and potentially alter the outcome of the program.
Snippet:
On Change of
Guideline: Avoid using On Change Of statement for Break processing
Explanation: For Internal table Loop processing, break level processing should be programmed with the AT....ENDAT
statements. Do not use ON CHANGE OF because it is specific to a single field. Unpredictable results
can occur with the ON CHANGE OF, whereas the AT... ENDAT triggers a break when any change
occurs in the specified field or fields left of the specified field.
Snippet:
ON CHANGE OF.
AT…ENDAT.
Case versus IF
Guideline: For Multiple conditions, use CASE
Explanation: Use the CASE statement rather than IF/ELSEIF/ENDIF statement when there are multiple conditions. If
there is only one condition, then the IF/ELSE/ENDIF statement is a good choice.
Snippet:
IF SY-SUBRC = 1.
code 1.
ELSEIF SY-SUBRC = 2.
code 2.
ELSE.
code others.
ENDIF.
CASE SY-SUBRC.
WHEN 1.
code 1.
WHEN 2.
code 2.
WHEN OTHERS.
code others.
ENDCASE.
Caution: DO NOT place a CHECK or EXIT statement in a user exit. If the CHECK or EXIT statement is
executed, all following user exits will not be executed.
In Summary: Use the correct Interrupt command to ensure the logic flow takes the path expected.
Snippet:
under construction
under construction
Exception Handling - after function call
Guideline: Generic Messaging after a function call is not recommended.
Explanation: Anytime you use the PATTERN function in the workbench to insert a function, it automatically inserts a
generic message in the format
“MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2, sy-msgv3, sy-
msgv4”.
This statement assumes that SY variables have been filled with meaningful values by the function itself.
However, often times, this is not the case.
The generic message should only be used in the case where the function and all nested functions that
are called use the Message...Raising format consistently throughout the function chain. The
Message...Raising format sets the sy-message and other sy variables. Only then, can you be assured
that a generic message statement can be used to present a proper error message. However, since this
situation is most likely not the case, a good developer will handle the message themselves and not count
on the generic form to provide a meaningful message relevant to the situation.
Snippet:
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty
NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
CASE sy-subrc.
WHEN 1.
MESSAGE e999 WITH 'Invalid Input'(101).
WHEN 2.
MESSAGE e999 WITH 'Invalid Material'(102).
WHEN 3.
MESSAGE e999 WITH 'No Materials'(103).
WHEN 4.
MESSAGE e999 WITH 'Other Reasons'(104).
ENDCASE.
IF SY-SUBRC <> 0.
write E100 with v_msg sy-subrc.
ENDIF.
This code is longer, yes, but not sensitive to date format changes which
can cause some very serious errors and outcome.
BEGIN OF S_MYDATE,
MONTH(2),
DAY(2),
YEAR(4),
END OF S_MYDATE .
BEGIN OF S_YOURDATE,
YEAR(4),
MONTH(2),
DAY(2),
END OF S_YOURDATE.
S_MYDATE-YEAR = S_YOURDATE-YEAR.
S_MYDATE-MONTH = S_YOURDATE-MONTH.
S_MYDATE-DAY = S_YOURDATE-DAY.
Structures
Deep Structure
Deep Structure
Data: BEGIN OF S_ORDER,
component1(1) type c,
component2 TYPE VBAK,
component3 type I,
Data: END OF S_ORDER.
Special Considerations:
To access the individual fields in a deep structure, please use syntax in the following example
S_ORDER-S_SO-VBELN = v_ordernumber.
Typing Structures
Typing Structures
TABLES
Guideline: TABLES statement is not required
Explanation: The use of TABLES statements for the declaration of work areas or when used in an SQL statement, is
not required. It allocates unnecessary storage. This is a pre 4.x convention
Snippet:
TABLES: EKPO.
Always use a work area or assign directly to a field symbol when processing the internal table.
Snippet:
Loop at itab.
* reference itab components directly via the header area
result = itab-field1.
Endloop.
Loop at itab.
* reference itab components directly via the header area
result = itab-field1.
Endloop.
EXTRACT files
Guideline: Combine multiple arrays of data with different structures into a single table for processing, an EXTRACT
file is an option
Explanation: There are advantages to using an Extract file but common practice is that most developers will tend to
use internal tables for array processing. It is a good option to use for reporting purposes when dealing
with internal tables of different structures that same same or similar keys.
Consideration: From a maintenance perspective, few developers have experience with EXTRACT files.
NOTE: Only one EXTRACT file can be used per program, whereas many internal tables could be used in
one program. There is no right or wrong on this one and is entirely up to the IT shop or the programming
requirement.
Boolean Operators
Guideline: Use operators (+, -, *, /, =) rather than the words ADD, SUBTRACT, MULTIPLY, DIVIDE, MOVE
Explanation: Use the Boolean operators rather than the alphabetic acronyms for boolean operations.
Note: This is not a strict and hard guideline. It is only meant to be a suggestion to adhere to boolean
operations over Engligh like commands.
Snippet:
value2 = value1.
IF items GT 0.
IF items > 0.
Dynamically called Function Modules must be defined in their own Function Group.
If Function Modules are being called ‘IN UPDATE TASK’ and are using internal tables that are defined as
Global Data, the global internal table will need to be cleared each time the function module is being
executed. Otherwise the internal table records are still available from the last function module execution
and could cause failures such as a "Duplicate Key Update Task".
Snippet:
under construction
perform calculate_difference.
FORM calculate_difference.
data: lv_amt type i,
lv_raise type i.
FORM calculate_difference.
data: lv_amt type i,
lv_raise type i,
lv_total type i.
Number Ranges
Guideline: Use the number range functions for generating unique sequence numbers
Explanation: A unique sequence number can be auto generated by SAP functionality. No need to build custom
sequence number generators. Use the SAP functions from function groups SNR* for this purpose.
Note: Number range objects are transportable objects but Never transport number range intervals.
Snippet:
ZSEQNO = ZSEQNO + 1.
Variant
Guideline: Transporting Variants
Explanation: Variants should be entered in a Gold client and then tranported thru the migration path to the target
environments.
DATA: v_myfield.
Always provide an action or a generic message so that the program can announce that an unexpected
situation has been encountered.
Snippet:
CASE SY-SUBRC.
WHEN 1.
code 1.
WHEN 2.
code 2.
ENDCASE.
CASE SY-SUBRC.
WHEN 1.
code 1.
WHEN 2.
code 2.
WHEN OTHERS.
MESSAGE W100.
ENDCASE.
Text Elements
Guideline: For multi-lingual prgramming, all text in quotes should have an associated text element defined
Explanation: Think Multilingual
If text literals are enclosed in quotes in statements like WRITE, MESSAGE, CONCATENATE , then there
should be an associated text element.
i.e. Write: "Error with this statement"(099)
- Do not use multiple text elements to generate one large text within your program.
- Use the addition 'AS TEXT' within SORT statements to ensure that the sort sequence considers
language specific characters, e.g. German ä, ü, ö, β .
- Use Text elements or report title to define list headings
- Text descriptions in the Data Dictionary should be defined in language dependent tables.
- Description fields defined in the Data Dictionary should be long enough to store translations.
Snippet:
Write Text-099
or
BAPI
De SAP ABAP en castellano
RFC
De SAP ABAP en castellano