ABAP - Keyword Documentation →  ABAP Programming Guidelines →  Robust ABAP →  Data Types and Data Objects → 

Declaration of Data Types and Constants

Background

Data types and constants can be declared in the following contexts:

Technically speaking, type groups, global classes and interfaces are also ABAP programs, in which data types and constants are created using the TYPES and CONSTANTS statements. In contrast to other ABAP programs, the declarations can also be statically visible in other programs, depending on the visibility section. Absolute type names enable you to dynamically access the types of all programs.

Rule

Declare data types and constants in the appropriate context

Declare data types and constants in the context that guarantees the best possible encapsulation.

Details

Data types and constants should be declared in the context in which they are visible for all potential consumers but not anywhere else:

Note

The misuse of include programs for the declaration of data types and data objects that can be reused across various programs is not allowed.

Bad Example

The following source code shows the declaration of constants in a type group that are required across different programs. The name of the type group must precede the names of the constants as a prefix. You should not create any new type groups. For constants that are required in various different programs, the above rule recommends a declaration in global classes or interfaces.

TYPE-POOL zicon.
  TYPES zicon_icon TYPE ...
  CONSTANTS:
    zicon_cancel      TYPE zicon_icon VALUE icon_cancel,
    zicon_check       TYPE zicon_icon VALUE icon_check,
    zicon_check_words TYPE zicon_icon VALUE icon_intensify,
    zicon_document    TYPE zicon_icon VALUE icon_hlp,
    zicon_download    TYPE zicon_icon VALUE icon_export,
    ...

Good Example

The following source code illustrates the declaration of the constants in the above example in a global class. The visibility of the constants is restricted to the current package. In other programs, the constants are addressed using cl_..._icons=>.

CLASS cl_..._icons DEFINITION PUBLIC FINAL.
  PUBLIC SECTION.
    TYPES  icon TYPE ...
    CONSTANTS cancel      TYPE icon VALUE icon_cancel.
    CONSTANTS check       TYPE icon VALUE icon_check.
    CONSTANTS check_words TYPE icon VALUE icon_intensify.
    CONSTANTS document    TYPE icon VALUE icon_hlp.
    CONSTANTS download    TYPE icon VALUE icon_export.
    ...
ENDCLASS.