Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Exit Method Loop

former_member2492
Active Participant
0 Likes
3,662

Hi all,

How can I exit a method in class from another method?

lets say I have the following:

method x.
loop at record into data(ls_record).
 me->check()."do smth inside here and if conditions are not met,continue with the next record
endloop.
endmethod.

Please let me know!

1 ACCEPTED SOLUTION
Read only

FredericGirod
Active Contributor
3,536

simple is always better

class ... definition ...
  methods check
    returning
      value(result) type abap_bool.
endclass.



class ... implentation.
method check.
  result = cond #( when sun->is_shining( ) eq abap_true then abap_true else abap_false).
endmethod.


method x.
loop at record into data(ls_record).
 check me->check() eq abap_true.
endloop.
endmethod.
endclass.
10 REPLIES 10
Read only

maheshpalavalli
Active Contributor
0 Likes
3,536

Your question implies that you want to exit method 'x'. But your comment next to check() method says that you want to continue to the next record in the loop without exiting the method x.. Or did I misunderstood the question?

Read only

FredericGirod
Active Contributor
3,537

simple is always better

class ... definition ...
  methods check
    returning
      value(result) type abap_bool.
endclass.



class ... implentation.
method check.
  result = cond #( when sun->is_shining( ) eq abap_true then abap_true else abap_false).
endmethod.


method x.
loop at record into data(ls_record).
 check me->check() eq abap_true.
endloop.
endmethod.
endclass.
Read only

matt
Active Contributor
3,536

740 SP8, you don't need the eq abap_true

LOOP AT record into data(ls_record).
  check the_record_has_passed_the_check( ls_record ).
ENDLOOP.
Read only

matt
Active Contributor
3,536

Funny thing - the clean code group don't like check, but I think it makes things clearer. Also, if you've got multiple checks - don't AND them together. It makes debugging harder - you can't figure out instantly which one failed - and in my view it's less clear.

CHECK does_it_pass_this_condition( ).
CHECK does_it_pass_that_condition( ).
CHECK does_it_pass_the_other_condition( ).
Read only

0 Likes
3,536

Agree

but ... your CHECKs have to be in a global CHECK method. Otherwise your method has several responsibilities.

and in clean code we have to used:

LOOP AT record REFERENCE INTO data(o_record).
  check the_record_has_passed_the_check( o_record->* ).
ENDLOOP.

but I am not sure all developers could understand this.

Read only

former_member2492
Active Participant
0 Likes
3,536

maheshkumar.palavalli no you are correct

Read only

Sandra_Rossi
Active Contributor
3,536

I prefer to use IS_OKAY instead of CHECK so that the code is easier to understand (EDIT: "IF subject verb comparison" is better than "IF verb is true"):

CHECK me->is_okay( ) = abap_true.

or

IF me->is_okay( ) = abap_false.
  CONTINUE.
ENDIF.

You may also use Predicative Method Call since 7.40 SP 8:

CHECK me->is_okay( ).

or

IF NOT me->is_okay( ).
  CONTINUE.
ENDIF.
Read only

0 Likes
3,536

I assume you used IS_<something>_OKAY ? otherwise CHECK or IS_OKAY are not clean code, We don't know what is OK or not

Read only

3,536
frdric.girod I agree that IS_OKAY is not good enough, but I don't know what is in OP's mind, I just think that "IF subject verb comparison" is better than "IF verb is true".
Read only

3,536

both answers are correct,thank you,I have upvoted your answer also