SAP / styleguides

This repository provides SAP style guides for coding and coding-related topics.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Prefer ENUM to constants interfaces

yunze-wang opened this issue · comments

Relevant sections of the style guide

Please link here to all relevant sections of the Clean ABAP guide

Description

Please describe here what changes to the above sections you propose

When using ENUM class, that would be a little bit overqualified, one class container can only list one type of value. I suggest using a combination of traditional 'Constants Interface' and Enum like below, so that we can:

  1. obey the classical guideline
  2. save repository artifacts cost
  3. referred type linkable
  4. group enum in a single structure

Examples

Please consider adding examples of patterns/anti-patterns this change suggests/avoids

INTERFACE if_some_business_area.
BEGIN OF ENUM ty_e_message_severity STRUCTURE cs_message_serverity,
error,
warning,
information,
success,
END OF ENUM ty_e_message_severity STRUCTURE cs_message_serverity.

when using this in somewhere, we can reference the type if_some_business_area=>ty_e_message_severity, the allowed value only comes from if_some_business_area=>cs_message_serverity-[error, ... success]

CLASS cl_some_function.
METHOD foo DEFINITION
IMPORTING message_severity type if_some_business_area=>ty_e_message_severity.

When call method
NEW cl_some_function( )->foo( if_some_business_area=>cs_message_serverity-error ).

The guide could possibly even go a step further and recommend CDS based enumerations instead, if available. Then no interface definition is needed and the constants can be used in CDS as well as ABAP.

https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abencds_define_enum_type.htm