"
Supported by

Gregarious constants

If you are one of those who insert the values directly in code instead of using constants, then read no more and go and stand over there in the corner on punishment for 1 hour to learn not to be lazy. If you’ve come back from punishment or you usually use constants, then please continue reading.

Imagine using the following constants:


CONSTANTS:
  C_BUKRS_ESPANHA TYPE BUKRS VALUE '1111',
  C_BUKRS_FRANCA   TYPE BUKRS VALUE '2222',
  C_BUKRS_INDIA       TYPE BUKRS VALUE '3333',
  C_MODE_ERRO        TYPE XFELD VALUE 'E',
  C_MODE_INFO         TYPE XFELD VALUE 'I',
  C_MODE_WARN       TYPE XFELD VALUE 'W'.

START-OF-SELECTION.

  CASE l_bukrs.
    WHEN C_BUKRS_ESPANHA.
      txt = 'Hola'.
   WHEN C_BUKRS_FRANCA.
     txt = 'Bonjour'.
   WHEN C_BUKRS_INDIA.
     txt = 'Namaste'.
  ENDCASE.
  WRITE txt.

As you can see there are two groups of constants which would ideally be grouped together, and they are not. They can be grouped as follows:


CONSTANTS:
  BEGIN OF C_BUKRS,
    ESPANHA TYPE BUKRS VALUE '1111',
    FRANCA TYPE BUKRS VALUE '2222',
    INDIA TYPE BUKRS VALUE '3333',
  END OF C_BUKRS,

  BEGIN OF C_MODE,
    ERRO        TYPE XFELD VALUE 'E',
    INFO         TYPE XFELD VALUE 'I',
    WARN       TYPE XFELD VALUE 'W',
  END OF C_MODE.

START-OF-SELECTION.

  CASE l_bukrs.
    WHEN C_BUKRS-ESPANHA.
      txt = 'Hola'.
   WHEN C_BUKRS-FRANCA.
     txt = 'Bonjour'.
   WHEN C_BUKRS-INDIA.
     txt = 'Namaste'.
  ENDCASE.
  WRITE txt.

Thus, the declaration makes much more sense because the constants are grouped logically. The use is almost identical. It is so obvious. Yet, I only discovered that this can be done a few days ago.

Thanks to suki suk for the photo.

Greetings from Abapinho.

2 comentários a “Gregarious constants”

  1. Custodio Diz:

    Mais uma excelente dica.

    No projeto em que estou trabalhando, costumamos usar uma classe apenas para as constantes, podendo assim reutilizar as constantes em outros programas. Exemplo:

    Na SE24 ou SE80, crie uma classe ZCL_CONSTANTES, definindo como FINAL.

    em atributos, crie:
    C_bukrs_espanha type bukrs_d value ‘1111’
    C_bukrs_franca type bukrs_d value ‘2222’
    C_bukrs_india type bukrs_d value ‘3333’

    todos eles tipo constant e visibilidade public

    no seu programa, use:

    START-OF-SELECTION.

    CASE l_bukrs.
    WHEN zcl_constants=>c_bukrs_ESPANHA.
    txt = ‘Hola’.
    WHEN zcl_constants=>c_bukrs_franca.
    txt = ‘Bonjour’.
    WHEN zcl_constants=>c_bukrs_INDIA.
    txt = ‘Namaste’.
    ENDCASE.
    WRITE txt.

    Pode tb criar uma unica constante estruturada, bastando clicar na seta de entrada direta de dados e declarar a contante como fez no exemplo:
    CONSTANTS:
    BEGIN OF C_BUKRS,
    ESPANHA TYPE BUKRS VALUE ‘1111’,
    FRANCA TYPE BUKRS VALUE ‘2222’,
    INDIA TYPE BUKRS VALUE ‘3333’,
    END OF C_BUKRS.

    no seu programa ficaria:

    START-OF-SELECTION.

    CASE l_bukrs.
    WHEN zcl_constants=>c_bukrs-ESPANHA.
    txt = ‘Hola’.
    WHEN zcl_constants=>c_bukrs-franca.
    txt = ‘Bonjour’.
    WHEN zcl_constants=>c_bukrs-INDIA.
    txt = ‘Namaste’.
    ENDCASE.
    WRITE txt.

  2. admin Diz:

    Olá Custodio, boa ideia, obrigado por partilhares.

Deixe um comentário


About Abapinho
Abapinho runs on WordPress
Articles (RSS) e Comments (RSS).