Skip to content

Latest commit

 

History

History
157 lines (122 loc) · 5.58 KB

ImplicitTypeRule.md

File metadata and controls

157 lines (122 loc) · 5.58 KB

<-- previous rule | overview | next rule -->

Make implicit type explicit

Replaces obsolete short forms of declarations with explicit forms that specify the TYPE, LENGTH, and DECIMALS (if applicable).

This rule is part of the essential profile, as it is explicitly demanded by the Clean ABAP Styleguide.

References

Options

  • Execute on TYPES statements
  • Execute on CONSTANTS and STATICS statements
  • Execute on DATA and CLASS-DATA statements
  • Replace parentheses with LENGTH

Examples

CLASS cl_implicit_type DEFINITION.
  PUBLIC SECTION.
    " in the object-oriented context, TYPES only allows for two short forms with implicit TYPE,
    " while more short forms (with implicit LENGTH and DECIMALS) are tolerated with (CLASS-)DATA, CONSTANTS, STATICS:
    TYPES: ty_b(5),
           ty_c LENGTH 5.

    TYPES: BEGIN OF ty_s_implicit,
             b(5),
             c LENGTH 5,
           END OF ty_s_implicit.

    " for built-in types c, n, and x, the default length is 1
    CONSTANTS: gc_a VALUE 'a',
               gc_b(5) VALUE 'abcde',
               gc_d TYPE c VALUE 'a',
               gc_e TYPE n VALUE '2',
               gc_f TYPE x VALUE ''.

    " the cleanup rule also ensures that each of the expanded declarations gets a line of its own
    CLASS-DATA:
      gv_a, gv_b(5),
      gv_d TYPE c, gv_e TYPE n, gv_f TYPE x,
      " for built-in type p (packed numbers), the default is LENGTH 8 DECIMALS 0
      gv_g TYPE p, gv_h(5) TYPE p, gv_i TYPE p LENGTH 3, gv_j TYPE p DECIMALS 4.

    CLASS-METHODS make_implicit_type_explicit.
ENDCLASS.

CLASS cl_implicit_type IMPLEMENTATION.
  METHOD make_implicit_type_explicit.
    CONSTANTS lc_text_length TYPE i VALUE 10.

    STATICS:
      st_g TYPE p VALUE '123456789012345-',
      st_h(5) TYPE p VALUE '123456789-',
      st_i TYPE p LENGTH 3 VALUE '12345-',
      st_j TYPE p DECIMALS 4 VALUE '12345678901.2345-'.

    " DATA allows for all short forms except 'x LENGTH n':
    DATA lv_a.
    DATA lv_b(5).
    DATA lv_d TYPE c.
    DATA lv_e TYPE n.
    DATA lv_f TYPE x.

    DATA lv_g TYPE p.
    DATA lv_h(5) TYPE p.
    DATA lv_i TYPE p LENGTH 3.
    DATA lv_j TYPE p DECIMALS 4.

    " the length may also be specified with a constant:
    DATA lv_k(lc_text_length).
    DATA lv_l(lc_text_length) TYPE c.
  ENDMETHOD.
ENDCLASS.

Resulting code:

CLASS cl_implicit_type DEFINITION.
  PUBLIC SECTION.
    " in the object-oriented context, TYPES only allows for two short forms with implicit TYPE,
    " while more short forms (with implicit LENGTH and DECIMALS) are tolerated with (CLASS-)DATA, CONSTANTS, STATICS:
    TYPES: ty_b TYPE c LENGTH 5,
           ty_c TYPE c LENGTH 5.

    TYPES: BEGIN OF ty_s_implicit,
             b TYPE c LENGTH 5,
             c TYPE c LENGTH 5,
           END OF ty_s_implicit.

    " for built-in types c, n, and x, the default length is 1
    CONSTANTS: gc_a TYPE c LENGTH 1 VALUE 'a',
               gc_b TYPE c LENGTH 5 VALUE 'abcde',
               gc_d TYPE c LENGTH 1 VALUE 'a',
               gc_e TYPE n LENGTH 1 VALUE '2',
               gc_f TYPE x LENGTH 1 VALUE ''.

    " the cleanup rule also ensures that each of the expanded declarations gets a line of its own
    CLASS-DATA:
      gv_a TYPE c LENGTH 1,
      gv_b TYPE c LENGTH 5,
      gv_d TYPE c LENGTH 1,
      gv_e TYPE n LENGTH 1,
      gv_f TYPE x LENGTH 1,
      " for built-in type p (packed numbers), the default is LENGTH 8 DECIMALS 0
      gv_g TYPE p LENGTH 8 DECIMALS 0,
      gv_h TYPE p LENGTH 5 DECIMALS 0,
      gv_i TYPE p LENGTH 3 DECIMALS 0,
      gv_j TYPE p LENGTH 8 DECIMALS 4.

    CLASS-METHODS make_implicit_type_explicit.
ENDCLASS.

CLASS cl_implicit_type IMPLEMENTATION.
  METHOD make_implicit_type_explicit.
    CONSTANTS lc_text_length TYPE i VALUE 10.

    STATICS:
      st_g TYPE p LENGTH 8 DECIMALS 0 VALUE '123456789012345-',
      st_h TYPE p LENGTH 5 DECIMALS 0 VALUE '123456789-',
      st_i TYPE p LENGTH 3 DECIMALS 0 VALUE '12345-',
      st_j TYPE p LENGTH 8 DECIMALS 4 VALUE '12345678901.2345-'.

    " DATA allows for all short forms except 'x LENGTH n':
    DATA lv_a TYPE c LENGTH 1.
    DATA lv_b TYPE c LENGTH 5.
    DATA lv_d TYPE c LENGTH 1.
    DATA lv_e TYPE n LENGTH 1.
    DATA lv_f TYPE x LENGTH 1.

    DATA lv_g TYPE p LENGTH 8 DECIMALS 0.
    DATA lv_h TYPE p LENGTH 5 DECIMALS 0.
    DATA lv_i TYPE p LENGTH 3 DECIMALS 0.
    DATA lv_j TYPE p LENGTH 8 DECIMALS 4.

    " the length may also be specified with a constant:
    DATA lv_k TYPE c LENGTH lc_text_length.
    DATA lv_l TYPE c LENGTH lc_text_length.
  ENDMETHOD.
ENDCLASS.

Related code