Skip to Content
avatar image
Former Member

ABAP OOP constant declaration best practice

We have about a dozen function groups and 30-40 function modules which all need to commonly reference upwards of 100 constants.

We currently do this:

include /namespace/constants

But, the extended code check tells me it is bad practice to have an include used in more than one function group.

We're not going to rewrite all of our code to sit in a single function group, like it proposes.

As we move to object oriented development, we face the same question.

Is it best to put all of the constants into a single class and then reference it in our code like this:

if ls_knvp-parvw = /namespace/cl_constant=>gc_soldto_function

That feels cumbersome, but manageable. Maybe we can alias it or something?

I'd like to use a type group, but we can't put it in a namespace and the usage seems to be deprecated?

Basically, I want something easy to use that can be referenced from multiple classes or function groups. Shorter naming is preferable. Passing the extended syntax check is very important.

Thanks for your suggestions!

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

4 Answers

  • Best Answer
    Feb 24, 2017 at 03:37 PM

    Having all your constants in one place, no matter whether you're writing something for Finance or for HR, is not good practice. The idea should be to be as specific as possible. Personally, I don't see anything wrong with having e.g.

    CONSTANTS c_end_of_time TYPE d VALUE '99991231'

    defined multiple times. If I didn't do it in a class, then I'd do it in an interface - but one constants interface per application. Having your constants defined in one or a few different places, used by many applications, to my mind is close to defining a global variable v_count for all your integer needs. I.e. not a good idea.

    Group your constants into related groups, define them in interfaces, then use aliases in the class definitions.

    Add comment
    10|10000 characters needed characters exceeded

  • Feb 24, 2017 at 05:45 PM

    The ABAP programming guidelines say: put'em to a class or interface. And hey, since 7.51 we have enumerations ...

    Add comment
    10|10000 characters needed characters exceeded

  • Feb 24, 2017 at 03:42 PM

    You can create instance of your "constant class". And it will shorten code for usage :-)

    Like this:

    DATA: lo_cs1 TYPE REF TO /namespace/cl_long_class_name.
    CREATE OBJECT lo_cs1.
    IF l_variable = lo_cs1=>c_constant.
    Add comment
    10|10000 characters needed characters exceeded

  • Feb 28, 2017 at 01:05 PM

    Hello Ray,

    as a constant is constant, so you are free to define both a global constant à la /namespace/cl_constant=>gc_soldto_function and a local scope redefinition:

    CONSTANTS c_soldto_function TYPE ... VALUE /namespace/cl_constant=>gc_soldto_function.


    Add comment
    10|10000 characters needed characters exceeded

    • Personally, I like this trick of aliasing constants (eventually using a custom-defined macro like "mac_alias

      /namespace/if_long_interface_name : sun, moon") . Of course, I would prefer enumerations from ABAP 7.50.