Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

ABAP Constants Interfaces - Naming conventions

Tomas_Buryanek
Product and Topic Expert
Product and Topic Expert
7,261

Hello,

NOTE: this is not regular "one answer is correct" question.

I would like to ask you what are your naming conventions in regards of "Constants Interfaces".

For example:

1) ZIF_C_AREA
2) ZIF_AREA_CONST
3) ZIF_AREA_CONSTANTS
4) ZIFC_AREA "But I think there should be _ after IF
...

I like #1 because it is short and Ctrl+Space after writing ZIF_C can show all constant interfaces available. But standard interfaces are using variants of naming #2 and #3 from what I searched...

What are your approaches if you want to share please? 🙂

-- Tomas --

Hello,

NOTE: this is not regular "one answer is correct" question.

I would like to ask you what are your naming conventions in regards of "Constants Interfaces".

For example:

1) ZIF_C_AREA
2) ZIF_AREA_CONST
3) ZIF_AREA_CONSTANTS
4) ZIFC_AREA "But I think there should be _ after IF
...

I like #1 because it is short and Ctrl+Space after writing ZIF_C can show all constant interfaces available. But standard interfaces are using variants of naming #2 and #3 from what I searched...

What are your approaches if you want to share please? 🙂

14 REPLIES 14
Read only

matt
Active Contributor
5,070

I don't care what the format of the name is as long as it's meaningful.

However, constants defined as interfaces are a bit of an anti-pattern, and can cause dependencies where you really don't want them. If you insist, it might be better to define constants in an abstract class as you can have complex structure "constants", like internal tables, set as read-only static variables, and initialised in the CLASS_CONSTRUCTOR.

Read only

Tomas_Buryanek
Product and Topic Expert
Product and Topic Expert
0 Likes
5,070

Thank you Matthew! I see I need to look at it in more detail.

-- Tomas --
Read only

vonglan
Active Participant
5,070

Hi Matthew,

the Wikipedia text is about "having classes implement that interface in order to achieve convenient syntactic access to those constants".

What do you think about using an ABAP Interface to collect constants like specific sales order types (AUART), that are not used in only one class, and then using them without using inheritance?

Best regards,

Edo

Read only

matt
Active Contributor
5,070

I think use static attributes of an abstract class.

Read only

joao_sousa2
Active Contributor
5,070

Don't use constants in interfaces, that's not what they are supposed to be used for.

Like Matthew said, if you need them for some internal logic, then what you really want is an abstract class from which you will inherit.

Read only

matt
Active Contributor
0 Likes
5,070

They're read-only static variables and constants; inheritance isn't necessary.

Read only

Florian
SAP Champion
SAP Champion
5,070

Constant Interfaces can also be useful. We have something similar to the switch framework and are using a Interface to handle the keys here. Of course we could also do it with an abstract class, but we prefered that way.

No argument why it's better, it's just because.

Regarding the naming convention we use Y(Z)_IF_CON_

But have to say, that there is only 1 (one!) constant interface in the whole system, so it's not really a naming convention 🙂

Read only

joao_sousa2
Active Contributor
5,070

An interface should be viewed as a contract with an outside party, and I think adding constants to this confuses the consumer as it indicates that the interface expects some sort of pre-specificied behavior. In what situations would the interface consumer require "constants"?

Even when I mention abstract classes, it's because the (private) constants can be used in non-abstract methods of the abstract class, so the person who will implement the non-abstract child won't need to understand the usage of it (or that they even exist).

Read only

vonglan
Active Participant
5,070

By the way, to allow finding the constant Interfaces easier, you could use a "tag Interface" like IF_BADI_INTERFACE in all of them.

Read only

Tomas_Buryanek
Product and Topic Expert
Product and Topic Expert
0 Likes
5,070

Interesting. Did not know about the "tag interface". Thank You!

ABAP documentation 752 on "tag interface"

-- Tomas --
Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
5,070

I would like to take a leaf out of BOPF's book. The constant interfaces end with _C e.g., /BOBF/IF_DEMO_SALES_ORDER_C.

But as Matt mentioned, as long as the name is meaningful i wouldn't scratch my head over it 🙂

matthew.billingham:

"... the constant interface pattern describes the use of an interface solely to define constants, and having classes implement that interface in order to achieve convenient syntactic access to those constants"

In ABAP we don't need to do this, do we?

You have said & i quote, "can cause dependencies where you really don't want them." Can you give an example of this?

 INTERFACE lif_shirt_sizes.
  TYPES:
    BEGIN OF ENUM size STRUCTURE mc_size,
      s,
      m,
      l,
      xl,
      xxl,
    END OF ENUM size STRUCTURE mc_size. " ENUM size
ENDINTERFACE.


CLASS lcl_demo DEFINITION CREATE PRIVATE.


  PUBLIC SECTION.
    CLASS-METHODS:
      main.
  PROTECTED SECTION.
  PRIVATE SECTION.


ENDCLASS.


CLASS lcl_demo IMPLEMENTATION.


  METHOD main.


    DATA: size TYPE lif_shirt_sizes=>size.


    cl_demo_input=>request(
      EXPORTING
        text        = |Shirt Size|
      CHANGING
        field       = size
    ).


    cl_demo_output=>display(
      SWITCH string( size
        WHEN lif_shirt_sizes=>mc_size-s
          THEN |Small|
        WHEN lif_shirt_sizes=>mc_size-m
          THEN |Medium|
        WHEN lif_shirt_sizes=>mc_size-l
          THEN |Large|
        WHEN lif_shirt_sizes=>mc_size-xl
          THEN |Larger|
        WHEN lif_shirt_sizes=>mc_size-xxl
          THEN |Largest|
        ELSE |Invalid size|
      )
    ).
  ENDMETHOD.


ENDCLASS.


START-OF-SELECTION.
  lcl_demo=>main( ).

joao.sousa2

In what situations would the interface consumer require "constants"? -> As a consumer of the "constant" interface i need to know the acceptable shirt-sizes (in this case the enumerated type SIZE).

Of course, i can define the enumeration in an abstract class as well. But i am failing to see the advantage of it. Maybe someone clarify

BR,

Suhas

Read only

5,070

In my view if you want an enum on shirt sizes, you should define them as the Size enum of the object Shirt. That way, the enumeration is connected to a object that has meaning, instead of an strange interface.

If your "Shirt" object doesn't really exist because you only deal with lower level abstractions, the Shirt can exist as a abstract class from which the other shirts can inherit. Again because it's an real-life object, and it makes sense to exist as a class.

I don't see a good reason to define Shirt_Sizes as an interface. Sure you can, but why would you?

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
5,070

In my view if you want an enum on shirt sizes, you should define them as the Size enum of the object Shirt. That way, the enumeration is connected to a object that has meaning, instead of an strange interface.

I agree with this, and it definitely makes more sense.

What if i need to select the "rack" of shirts depending on the "size" of the shirt? Basically if i want to use the "size" in different classes, should that enum be defined in an abstract class of "shirt" as well?

Read only

pokrakam
Active Contributor
5,070

My 2p: An interface has a very small advantage of not loading any code into program memory. But being able to override constants e.g. for test purposes does make classes more versatile.

As naming goes, if the intended usage is purely for constants, I personally prefer a more meaningful name that deliberately drops CL or IF to show its not a 'functioning' class or interface. And I like to group related constants into a structure so we can pick the relevant one via ctrl-space. Thus:

zorder_const=>status-open.

This also allows you to change an interface to a class without too much fuss at a later stage.

By the way, in Eclipse (not sure about SE80) you can use wildcards with ctrl-space. So type z*const and hit ctrl-space will find the above.