ABAP Forum
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

ABAP syntax advice

jeff_broodwar
Active Participant
0 Likes
78

Hi Experts,

I found some codes and wondered which is best?

1. I read before during in earlier ECC to avoid this because of headerline being obsolete in the future.

loop at s_bukrs into data(lwa_bukrs).
  append lwa_bukrs-low to gt_bukrs.
endloop.

"Compared to:

gt_bukrs[] = s_bukrs[].

 2. Similar to above, I still some people use the longer method compare to the shorter below:

loop at <fs_bukrs> assigning <fs_range>
append value#(
sign = <fs_range>-sign
option = <fs_range>-option
low = <fs_range>-low
high = <fs_range>-high
)
to r_bukrs.
endloop.

r_bukrs[] = <fs_bukrs>[].

3.  Then found this one:

gt_bukrs = VALUE #( FOR lwa_bukrs IN s_bukrs ( lwa_bukrs-low ) ).

"Below is shorter...

gt_bukrs[] = s_bukrs[].

In no. 3, why do we still use for if line 5 is shorter?

Appreciate your feedback on these samples.

@AlexandrePassarelli

Thank you.

 

Geoff

Hi Experts,

I found some codes and wondered which is best?

1. I read before during in earlier ECC to avoid this because of headerline being obsolete in the future.

loop at s_bukrs into data(lwa_bukrs).
  append lwa_bukrs-low to gt_bukrs.
endloop.

"Compared to:

gt_bukrs[] = s_bukrs[].

 2. Similar to above, I still some people use the longer method compare to the shorter below:

loop at <fs_bukrs> assigning <fs_range>
append value#(
sign = <fs_range>-sign
option = <fs_range>-option
low = <fs_range>-low
high = <fs_range>-high
)
to r_bukrs.
endloop.

r_bukrs[] = <fs_bukrs>[].

3.  Then found this one:

gt_bukrs = VALUE #( FOR lwa_bukrs IN s_bukrs ( lwa_bukrs-low ) ).

"Below is shorter...

gt_bukrs[] = s_bukrs[].

In no. 3, why do we still use for if line 5 is shorter?

Appreciate your feedback on these samples.

@AlexandrePassarelli

Thank you.

 

Geoff

4 REPLIES 4
Read only

TMNielsen
Contributor
0 Likes
41

In number 3 the short and long line are not doing the same thing.

You should also show us the definition of all the ranges and tables. Then we will be able to answer.

Read only

0 Likes
22

I see it the same way, asuming that the tabels are of type RANGE
the LONG version is only moving the field LOW,
while the short version in 3 is moving all columns.

If you want to move all, then use the short version in 3, as this is the most readable coding then

Read only

Joery
Participant
0 Likes
32

In general I will always use the short version as long as it remains readable. Too many nested short notations can sometimes make the code hard to understand.
Performance is usually the same or very similar while readability improves.

  1. When using a loop of any kind the best practice would be to use ASSIGNING instead of INTO
    In general it is best to avoid loops when possible 

  2. Use the short version if the field symbol has the same fields as the range table, if that is not the case I would go with a 'VALUE #( FOR ...' statement

  3. As @TMNielsen mentioned, the lines are doing different things since the first option only assigns the low value while the second copies all fields from table one to table two
Read only

VXLozano
Active Contributor
0 Likes

Tt's personal taste: I use INTO when I want not to modify data, and ASSIGNING when I want... it helps (me) no know if the LOOP will change data or will not with just a glance.

The rest, is everythingn about readability: if you are not comfy with the new syntax, you can try hard to become, or just ignore them. I'm just getting used to most of the new syntax and decided to ignore some of them (like that FOR you've put in the third sample).

About the new options: if the tables are not identical, you can use CORRESPONDING to copy them without FORs.