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

Feature Request: Replace EXIT with RETURN outside of loops

Open jan-jezek opened this issue 1 year ago • 3 comments

IF foo = 42.
    EXIT.
ENDIF.

LOOP AT bar ASSIGNING FIELD-SYMBOL(<bar>).
    IF <bar> = 42.
        EXIT.
    ENDIF.
ENDLOOP.

WRITE 'SOMETHING'.

The first EXIT should be replaced with RETURN (https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-US/abenexit_procedure_guidl.htm) the second one should not be replaced.

jan-jezek avatar Mar 19 '24 13:03 jan-jezek

Hi Jan,

that's a very nice idea – and shouldn't be too difficult to implement!

Kind regards, Jörg-Michael

jmgrassau avatar Mar 25 '24 14:03 jmgrassau

Note that replacing EXIT with RETURN inside an INCLUDE should probably be avoided. I've seen a lot of code where an INCLUDE is wrapped inside an a "DO 1 TIMES...ENDDO" to enable "return-like" behavior via use of EXIT. In this situation, replacing the EXIT with RETURN would also exit the Report and prevent execution of any subsequent code.

Example, replacing EXIT in sample below is not desireable:

REPORT Customer_Report.

DO 1 TIMES.
  INCLUDE zcustomer_enhancement.
ENDDO.

INCLUDE functions01.
INCLUDE zcustomer_enhancement.

if is_enhancement_active( lv_company_code ) = abap_false.
  exit.
endif.

"Enhancement logic below for company codes where it is activated.....

It's not pretty, but I understand why this technique is used because you can avoid having to wrap the whole code inside a big IF-condition.

samkumo avatar Jul 09 '24 10:07 samkumo

Hi Samuli,

good point, thanks! I think like the rule "Convert CHECK outside loop to IF NOT ... RETURN", ABAP cleaner should only do changes here if it can actually "see" the METHOD statement (which it wouldn't in an include).

Kind regards, Jörg-Michael

jmgrassau avatar Jul 10 '24 14:07 jmgrassau