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

Feature Request: APPEND -> INSERT

Open Xexer opened this issue 1 year ago • 9 comments

A nice feature would be, to refactor the APPEND statements into the INSERT version, to be independant from table types.

" Old version
APPEND ls_structure TO lt_table.
APPEND VALUE #( field = '' ) TO lt_table.

" New version
INSERT ls_structure INTO TABLE lt_table.
INSERT VALUE #( field = '' ) INTO TABLE lt_table.

Should also be formatted with VALUE rules.

Xexer avatar May 18 '23 09:05 Xexer

If this is done automatically than you can no longer explicitly state in the code that the line is added at the end. Which is allowed according to the guide.

https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#prefer-insert-into-table-to-append-to

fabianlupa avatar May 19 '23 08:05 fabianlupa

Could be an optional thing, appending entries to the end work fine with standard tables and empty keys. The use of the simple appends are very rare. This will help most people migrating their code to better versions and think about correct keys for tables.

Xexer avatar May 19 '23 11:05 Xexer

Hi Xexer and Fabian,

nice idea! I agree that this should rather be an opt-in rule (i.e. deactivated by default); but if the style guide says "Prefer INSERT INTO TABLE to APPEND TO", this is at least a good reason to offer such a rule!

Kind regards, Jörg-Michael

jmgrassau avatar May 21 '23 13:05 jmgrassau

There are however two pitfalls, when converting APPEND to INSERT:

  • By sorted tables with non-unique key appended lines are in the appending order, while inserted lines are in the reverse order.
TYPES: BEGIN OF line,
  number TYPE i,
  description TYPE string,
END OF line.

DATA sorted_table TYPE SORTED TABLE OF line WITH NON-UNIQUE KEY number.

APPEND VALUE #( number = 1  description = `Will be third` ) TO sorted_table.
APPEND VALUE #( number = 1  description = `Will be fourth` ) TO sorted_table.

INSERT VALUE #( number = 1  description = `Will be second` ) INTO TABLE sorted_table.
INSERT VALUE #( number = 1  description = `Will be first` ) INTO TABLE sorted_table.
  • By character lines APPEND writes from left to right ignoring the structure. By INSERT (and in both cases when using VALUE) inappropriate structure leads to an syntax error.
TYPES: BEGIN OF line_a,
  component_1 TYPE c LENGTH 2,
  component_2 TYPE c LENGTH 4,
END OF line_a,
BEGIN OF line_b,
  component_1 TYPE c LENGTH 4,
  component_2 TYPE c LENGTH 2,
END OF line_b.

DATA: structure_a TYPE line_a,
      table_b     TYPE STANDARD TABLE OF line_b.

structure_a-component_1 = '11'.
structure_a-component_2 = '2222'.

APPEND structure_a TO table_b.

m-badura avatar Sep 28 '23 23:09 m-badura

I've seen too much code working with unsorted table types and expecting the table entries to be appended. Such a rule wouldn't be useful to me as it would be too risky. I hope you'll get some use out of it though 🙂

ConjuringCoffee avatar Sep 29 '23 07:09 ConjuringCoffee

I've seen too much code working with unsorted table types and expecting the table entries to be appended. Such a rule wouldn't be useful to me as it would be too risky. I hope you'll get some use out of it though 🙂

But for standard (=unsorted?) tables there is no difference, both instructions do the same, don't they?

m-badura avatar Sep 29 '23 07:09 m-badura

But for standard (=unsorted?) tables there is no difference, both instructions do the same, don't they?

You're right. Here's a quote from the documentation:

For standard tables, each new row is appended as the last row in the internal table regardless of the primary table key.

I don't know why I thought it did something else? 😅 I guess I could actually get some use out of the suggested rule.

ConjuringCoffee avatar Sep 29 '23 07:09 ConjuringCoffee

Hi Xexer and Fabian,

nice idea! I agree that this should rather be an opt-in rule (i.e. deactivated by default); but if the style guide says "Prefer INSERT INTO TABLE to APPEND TO", this is at least a good reason to offer such a rule!

Kind regards, Jörg-Michael

As an opt-in rule, I see no reason not to do this., notwithstanding m-badura's examples.

matthewdjb avatar Jun 12 '24 07:06 matthewdjb

I've been thinking; since the Clean Code style guide says:

Use APPEND TO only if you use a STANDARD table in an array-like fashion, if you want to stress that the added entry shall be the last row.

m-badura's examples refer to SORTED tables, they're not really relevant.

matthewdjb avatar Jun 25 '24 07:06 matthewdjb