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

Replace MOVE-CORRESPONDING

Open Stefanov97 opened this issue 1 year ago • 5 comments

First of all - Great Job for what you have so far! I really appreciate it. Here is one idea from me:

according to the new features in ABAP syntax MOVE-CORRESPONDING should be replaced with the following:

For table: lt_adrt = CORRESPONDING #( lt_adrt ).

For structures: ls_result = CORRESPONDING #( lt_t005k_result[ 1 ] ).

Thank you!

Stefanov97 avatar Mar 11 '24 14:03 Stefanov97

Hi Pavel,

thanks for opening this issue! This was indeed already proposed in #36, but it is nevertheless good to have a dedicated issue for possible discussions.

As you can see in the ABAP documentation chapter on CORRESPONDING, Basic Form, Addition ... BASE ( base ) ..., MOVE-CORRESPONDING would more precisely have to be replaced in the following way:

" possible replacement for structures
MOVE-CORRESPONDING struct1 TO struct2.
struct2 = CORRESPONDING #( BASE ( struct2 ) struct1 ).

" possible replacement for internal tables
MOVE-CORRESPONDING itab1 TO itab2 KEEPING TARGET LINES.
itab2 = CORRESPONDING #( BASE ( itab2 ) itab1 ).

Without BASE, the result would be significantly different, because for structures, MOVE-CORRESPONDING only overwrites components that appear in both structures, but keeps all others unchanged, while the CORRESPONDING # constructor expression would return initial values for all components that are not found in struct1.

This makes me hesitate a bit, because with BASE, the statement actually looks more complex and less readable. What's more important, though, is that ABAP cleaner might not be able to determine in all cases whether the variables represent structures or tables (obviously we can't rely on naming conventions, and the definition of the variables might be out of sight for ABAP cleaner). Therefore, if you find

MOVE-CORRESPONDING a1 TO a2.

you could only change it into

a2 = CORRESPONDING #( BASE ( a2 ) a1 ).

if you are sure that a1 and a2 are structures. If they were table, this would change the behavior of the statement, because the original statement didn't have KEEPING TARGET LINES.

Therefore, not entirely sure yet whether (or under what exact conditions) this would be possible.

Kind regards, Jörg-Michael

jmgrassau avatar Mar 15 '24 08:03 jmgrassau

How about having an option to only execute the rule if the target variable wasn't set beforehand? For all other cases, I would prefer to keep it as MOVE-CORRESPONDING.

ConjuringCoffee avatar Mar 15 '24 08:03 ConjuringCoffee

Hi ConjuringCoffee,

yes, that might work – or if the target variable was just CLEARed in the previous line.

Kind regards, Jörg-Michael

jmgrassau avatar Mar 15 '24 09:03 jmgrassau