abap-cleaner icon indicating copy to clipboard operation
abap-cleaner copied to clipboard

Rearrange local declarations: sorting not kept as defined in rule settings

Open ziegleran opened this issue 6 months ago • 2 comments

Hello,

I have set up the rule 'Rearrange local declarations' in the following way: Image

Now, I have a method implementation, which defines some import parameters typed as table types.

methods my_method_name
  importing
    it_import_test type standard table of tadir.

Within the method, I have the following declaration part sorted in an opinionated order:

data ls_import_test like line of it_import_test.
data lv_some_other_value type i.

I often use ABAPCleaner for the whole class. Then it gets reformatted to:

data lv_some_other_value type i.
data ls_import_test like line of it_import_test.

It also resorts, if the complete method is selected (including the method- and endmethod-statements). Selecting the declaration block only doesn't reformat.

Based on issue #230, I understand, that variable-definitions using like have to be sorted below type, because the definition has to exist before. But in my example, the referenced definition comes from the method definition and not from the method implementation itself.

I can rearrange manually and activate the source code. But what confused me, that I use the settings as shown above and the definitions are rearrange anyhow.

Would it technically be possible to recognize also this constellation? Could it be fixed with one of the future releases?

Thanks and kind regards, Andreas Ziegler

ziegleran avatar Jun 28 '25 08:06 ziegleran

Hi Andreas,

I don't think this is a defect: If technically possible, the cleanup rule "Rearrange local declarations" uses the order in which variables are used inside the method implementation. Therefore, considering the following code:

Image

  • In any_method, the declaration lv_some_other_value is moved to the first position, because this variable is used first in the rs_result computation.
  • By contrast, in other_method, the rs_result computation first refers to ls_import_test, therefore, it is now kept as the first declaration. Note that only the method implementation is considered for the "order of appearance", not the method definition (which could anyway be "out of sight" if the method is defined in a parent class or an interface)
  • Technically, either way works, because the two declaration lines are independent.
  • However, in third_method, the declaration of ls_any_struc uses the declaration of lt_any_table and therefore must not be moved above it, even though ls_any_struc is used first in the code.

So, declarations using LIKE do not necessarily have to go second, but only if they use a local declaration.

Kind regards, Jörg-Michael

jmgrassau avatar Jul 02 '25 17:07 jmgrassau

P.S.: Sorry, I forgot the screenshot of third_method:

Image

And here's the code for further experiments:

  METHOD any_method.
    DATA ls_import_test LIKE LINE OF it_import_test.
    DATA lv_some_other_value TYPE i.

    rs_result = lv_some_other_value + lines( ls_import_test ).
  ENDMETHOD.


  METHOD other_method.
    DATA ls_import_test LIKE LINE OF it_import_test.
    DATA lv_some_other_value TYPE i.

    rs_result = lines( ls_import_test ) + lv_some_other_value.
  ENDMETHOD.


  METHOD third_method.
    DATA lt_any_table TYPE ty_tt_any.
    DATA ls_any_struc LIKE LINE OF lt_any_table.

    rs_result = ls_any_struc-value + lines( lt_any_table ).
  ENDMETHOD.

Kind regards, Jörg-Michael

jmgrassau avatar Jul 02 '25 18:07 jmgrassau