styleguides icon indicating copy to clipboard operation
styleguides copied to clipboard

Returning a value

Open Quappel opened this issue 3 years ago • 9 comments

In the guideline, as well as all trainings, it is said that one should name the return value result and then assign the return parameter in the code.

CLASS /clean/string_utils DEFINITION [...].
  CLASS-METHODS trim
   IMPORTING
     string TYPE string
   RETURNING
     VALUE(result) TYPE string.
ENDCLASS.

METHOD retrieve.
  DATA(trimmed_name) = /clean/string_utils=>trim( name ).
  result = read( trimmed_name ).
ENDMETHOD.

//not mentioned
METHOD retrieve.
  DATA(trimmed_name) = /clean/string_utils=>trim( name ).
  return read( trimmed_name ).
ENDMETHOD.

In other languages the form return Value; is used. This works also in Abap ( at least in the newest version ), I find it much easier to read. Why is it not mentioned? Also in the Language Help it is not mentioned. Is it a bug, is the feature coming soon?

Quappel avatar Nov 07 '22 09:11 Quappel

Where did you this form of return? What is the ABAP release you are on?

My gut feeling is someone defined a macro with the same name as return. Could you check the table TRMAC with name = 'RETURN'?

~Suhas

suynwa avatar Nov 17 '22 11:11 suynwa

Hi I am just back from vacation and will check tomorrow

Quappel avatar Nov 30 '22 10:11 Quappel

The ABAP release is 751.

I checked the TRMAC and there is no entry for 'RETURN'.

Quappel avatar Dec 01 '22 07:12 Quappel

You also cannot redefine keywords using macros (I assume this also applies to TRMAC) "RETURN" is an ABAP key word and cannot be redefined as a DEFINE macro.. I cannot reproduce this on 7.56 and the info doesn't quite add up for me.

This works also in Abap ( at least in the newest version )

is the feature coming soon?

The ABAP release is 751.

7.51 was released 6 years ago.

fabianlupa avatar Dec 01 '22 07:12 fabianlupa

Ohh sorry, I had no idea how to look it up and must have been looking at the wrong value. My coworker just told me that it is 7.58

Quappel avatar Dec 01 '22 07:12 Quappel

Ah now it makes sense, then we externals got another sneak peek with this styleguide repo ;)

(S/4 2022 was just released with 7.57 and the BTP ABAP Environment is on the equivalent 7.89.)

fabianlupa avatar Dec 01 '22 07:12 fabianlupa

Let's wait till that ABAP version is available to the external world, then recommend it as the default variant.

HrFlorianHoffmann avatar Dec 12 '22 14:12 HrFlorianHoffmann

As 7.58 is already officially supported, can this be added as recommended variant?

blackfish5 avatar Jun 10 '24 20:06 blackfish5

This is interesting, and appears to work on A4H 2022 / 7.58

Now a question is whether this is a recommended approach? The ABAP Doco says: The statement RETURN is generally intended for early but proper returns from processing blocks. This implies not to use it at the end of a method.

But it does read nicely, and eliminates the need for naming the result variable anywhere other than the declaration. I like this simplification. I mean it would be awkward if we compacted the result assignment into a RETURN in the middle of a method and not at the end:

METHOD calculate_stuff. 
  IF someattribute < 0. 
    RETURN -1.
  ENDIF.
  ...
  result = a + b.
ENDMETHOD.

So for consistency I'd rather use RETURN throughout:

METHOD calculate_stuff. 
  IF someattribute < 0. 
    RETURN -1.
  ENDIF.
  ...
  RETURN a + b.
ENDMETHOD.

pokrakam avatar Jun 10 '24 21:06 pokrakam