Skip to content

Enumerations must contain a unique value when using the object pattern #368

Open
@Codenarski

Description

@Codenarski

Proposed Rule
In some cases, it can be enough to have references to enum classes for distinctions. The whole thing works fine as long as you are within the same thread.

CLASS /clean/message_severity DEFINITION PUBLIC CREATE PRIVATE FINAL.

  PUBLIC SECTION.

    CLASS-DATA warning TYPE REF TO /clean/message_severity READ-ONLY,
    CLASS-DATA error   TYPE REF TO /clean/message_severity READ-ONLY.

    DATA value TYPE symsgty READ-ONLY.

    CLASS-METHODS class_constructor.
    METHODS constructor IMPORTING value TYPE symsgty.

ENDCLASS.

CLASS /clean/message_severity IMPLEMENTATION.

  METHOD class_constructor.
    warning = NEW #( 'W' ).
    error = NEW #( 'E' ).
  ENDMETHOD.

  METHOD constructor.
    me->value = value.
  ENDMETHOD.

ENDCLASS.

Anti Pattern:

CLASS /clean/message_severity DEFINITION PUBLIC CREATE PRIVATE FINAL.

  PUBLIC SECTION.

    CLASS-DATA warning TYPE REF TO /clean/message_severity READ-ONLY,
    CLASS-DATA error   TYPE REF TO /clean/message_severity READ-ONLY.

    CLASS-METHODS class_constructor.
    METHODS constructor IMPORTING value TYPE symsgty.

ENDCLASS.

CLASS /clean/message_severity IMPLEMENTATION.

  METHOD class_constructor.
    warning = NEW #(  ).
    error = NEW #(  ).
  ENDMETHOD.

  METHOD constructor.
    me->value = value.
  ENDMETHOD.

ENDCLASS.

Justification

If you pass an instance to a parallel process, for example, or serialize/deserialize this instance, there is a problem if you compare the references with each other because they are no longer the same. The instance passed from outside and the instance newly created via the class_constructor differ, although in both cases it is an instance of e.g. Warning. To prevent such problems, an enum Instance should always contain a value that can be checked against.

Metadata

Metadata

Assignees

No one assigned

    Labels

    New RuleThe issue or PR proposes an new rule or set of rulesclean-abap

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions