
ABAP OO - Usage of constants
Constants are an important component in ABAP development, both for reuse and for defining interfaces. Let's dive into the topic.
Table of contents
In this article, we'll look at the definition of constants, their use in development, and how you can easily access the corresponding values.
Introduction
Constant values are a central component in development when it comes to using interfaces. For example, you can use them to map different content options and specify the options to the developer using them. This eliminates the need to guess which values should be passed to which parameters, reducing the likelihood of errors. If, for example, the content i.e., the actual value of a constant changes, you can do so at a technical level without causing inconsistency in your colleagues' development.
Definition
In this chapter, we'll look at the definition of constants.
Individual Values
In principle, we can define each constant individually; this makes sense for individual values, since there is no connection between different values. However, we often find the variant in the standard that groups are formed that have the same beginning. In this example, we define three constants that represent different options for a status.
CONSTANTS option_created TYPE basic_option VALUE 'C'.
CONSTANTS option_planned TYPE basic_option VALUE 'P'.
CONSTANTS option_finished TYPE basic_option VALUE 'F'.
Groups
An alternative to single groups is defining a structure. Using BEGIN OF and END OF, we can create a group of constants and bundle them under one access. This makes definition, assignment, and use somewhat easier. Another developer will find the related values much easier. To do this, we define the individual values from above again in a structure.
CONSTANTS:
BEGIN OF options,
created TYPE basic_option VALUE 'C',
planned TYPE basic_option VALUE 'P',
finished TYPE basic_option VALUE 'F',
END OF options.
ENUM
A very special form of constants and structures is enumeration. It allows you to define constant values with a precise check of their contents. The compiler can then determine whether a value is permissible for an interface or not. In this article, we will now address the topic. If you would like more information, you can check out this article on the topic.
With the Simple Types from the Core Data Services area and the Table Entities, enumerations will be used more frequently in the future, as they will work from the database to the UI and there will be no break in the logic, as is currently the case with ENUMs.
Usage
This chapter deals with the use of constants during development. We'll cover two different ways of using it.
Interface
The first and most commonly used variant is to go through the corresponding interface, where the constants and groups are also defined. We call the interface, call the group, and then the corresponding field or constant of the structure.
IF demo->evaluate_option( zif_bs_demo_oo_constants=>options-created ).
ENDIF.
The group of constants gives us a quick overview of the different values that are possible for the OPTION. With the individual values, we must be careful to group them into pseudo-groups and then find them again. With many constants, the overview could suffer, as in the following example.
Hint: You can find more information in the Clean ABAP Guidelines.
Object
A second option, which we have not found so often so far, is the use of the of the actual object you're currently working with. In this case, we find our options directly after DEMO and can access them. We don't need to know or specify the interface, which makes the notation somewhat shorter.
IF demo->evaluate_option( demo->options-planned ).
ENDIF.
Since the constants are defined in the interface, they are also available in the object. This coupling allows us to use them directly.
Validation
Unlike ENUMs, however, you must perform the validation for constants yourself. Either you set the values in the process, or you should validate the values passed to the method. Otherwise, errors due to unknown values may occur in the subsequent process. In the worst case, this could cause processing to freeze and post-processing to become impossible.
Complete example
We have stored both resources in the corresponding GitHub repository, where you will find the two objects of the article in the ZBS_DEMO_OO_INTERFACE package. You can view the changes from this article via the commit.
Conclusion
Constants are an important component when working with interfaces and method interfaces. Even with their definition, you lay the foundation for quick and easy use for you and other developers.