‎2020 Mar 03 9:00 AM
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!
‎2020 Mar 03 9:20 AM
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.
‎2020 Mar 03 9:18 AM
‎2020 Mar 03 9:20 AM
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.
‎2020 Mar 03 10:25 AM
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.
‎2020 Mar 03 10:27 AM
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( ).
‎2020 Mar 03 10:31 AM
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.
‎2020 Mar 03 9:20 AM
‎2020 Mar 03 9:22 AM
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.
‎2020 Mar 03 9:26 AM
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
‎2020 Mar 03 9:31 AM
‎2020 Mar 03 9:35 AM
both answers are correct,thank you,I have upvoted your answer also