"
Supported by

More RANGEs, less SELECTs

The dictionary has the following entry for “style”: “set of formal aspects and expressive resources that characterise a text.”

It is preferable to read a text with style than one without. If ABAP were a language, a program in ABAP would be a text. When it comes to style, there are programs that seem to have been written by the feet, while there are others that one would swear the quill that wrote them was guided by the pure hand of an eighteenth century lady plagued by the troubles of love. Fortunately, I think it’s not possible to write programs in ABAP as tacky as the last sentence.

The “style” label is used here in Abapinho to identify articles that talk about just that: style. These articles try to find ways to improve the style of ABAP programs. This is one of Abapinho’s favourite labels, as you can see here. And what you are reading is another of those articles.

Moving on.

It is very common to see code something like this:


IF i_kunnr IS INITIAL.
  IF i_name1 IS INITIAL.
    SELECT SINGLE * INTO w_kna1 FROM kna1.
  ELSE.
    SELECT SINGLE * INTO w_kna1 FROM kna1 
      WHERE name1 = i_name1.
  ENDIF.
ELSE.
  IF i_name1 IS INITIAL.
    SELECT SINGLE * INTO w_kna1 FROM kna1 
      WHERE kunnr = i_kunnr.
  ELSE.
    SELECT SINGLE * INTO w_kna1 FROM kna1 
      WHERE kunnr = i_kunnr AND name1 = i_name1.
  ENDIF.
ENDIF.

In other words, we have two optional parameters that we can use to select a KNA1 register, and we perform the appropriate SELECT depending on whether they are filled in. Does it have style? No. I will show you another way to do the same:


DATA: r_kunnr TYPE RANGE OF kunnr,
          s_kunnr LIKE LINE OF r_kunnr,
          r_name1 TYPE RANGE OF name1,
          s_name1 LIKE LINE OF r_name1.

IF i_kunnr IS NOT INITIAL.
  s_kunnr-option = 'EQ'.
  s_kunnr-sign = 'I'.
  s_kunnr-low = i_kunnr.
  APPEND s_kunnr TO r_kunnr.
ENDIF.

IF i_name1 IS NOT INITIAL.
  s_name1-option = 'EQ'.
  s_name1-sign = 'I'.
  s_name1-low = i_name1.
  APPEND s_name1 TO r_name1.
ENDIF.

SELECT SINGLE * INTO w_kna1 FROM kna1 
  WHERE kunnr IN r_kunnr AND name1 IN r_name1.

How about this? It does the same thing but with much more style.

Another way would be to use a dynamic SELECT, but that would be unnecessarily complicated for this scenario.

Greetings from Abapinho.

9 comentários a “More RANGEs, less SELECTs”

  1. Thiago Alves Diz:

    Interessante, nunca tinha pensado dessa forma…!

  2. admin Diz:

    Pensamento lateral ;)

  3. Hugo Diz:

    Muito bom
    Seu texto tem muito estilo =D

  4. Nuno Godinho Diz:

    Ah ah obrigado :)

  5. Sara Diz:

    Boa dica!!!

  6. Paulo Diz:

    Mas NOT IN nao funciona muito bem com ranges. Ex: se nos parametros eu coloco que só quero o campo X = vazio, o select options fica assim:

    SIGN OPTION LOW HIGH
    I EQ

    se fizer por exemplo um DELETE WHERE NOT IN, isso nao funcionará (nao deletará nada).

  7. Nuno Godinho Diz:

    Olá Paulo, compreendo, se precisares de usar a negativa isto não funciona. Não sabia. E se se usar o SIGN=E em vez de SIGN=I e se fizer à mesma DELETE WHERE IN, não funcionará?

  8. Will Agner Diz:

    DATA: r_kunnr TYPE RANGE OF kunnr WITH HEADER LINE.

    IF i_kunnr IS NOT INITIAL.
    r_kunnr-option = ‘EQ’.
    r_kunnr-sign = ‘I’.
    r_kunnr-low = i_kunnr.
    APPEND r_kunnr TO r_kunnr.
    ENDIF.

  9. Nuno Godinho Diz:

    Olá Will. Não sei se compreendi. Ao usar um HEADER LINE ao RANGE o objectivo era poupar uma variável usando apenas o próprio R_KUNNR? É que HEADER LINE não é suportado em ABAP Objects e é considerado obsoleto na linguagem ABAP.

    De facto, era comum há muitos anos atrás usar-se o HEADER LINE. Mas a sua utilização foi descontinuada pois é considerada má práctica por, desta forma, o R_KUNNR se torna ambíguo (pois adopta dois comportamentos distintos dependendo do contexto) tornando o código mais obscuro e confuso. Eu já tinha deixado de o usar mesmo antes de ter passado a programar inteiramente com classes.

Deixe um comentário


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