styleguides icon indicating copy to clipboard operation
styleguides copied to clipboard

CHECK vs RETURN, example

Open larshp opened this issue 3 years ago • 7 comments

in https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#check-vs-return

the good example

METHOD read_customizing.
  IF keys IS NOT INITIAL.
    " do whatever needs doing
  ENDIF.
ENDMETHOD.

Might not be super good, especially it introduces nesting, https://twitter.com/JelenaAtLarge/status/1527775392790204416

I typically have code looking like

METHOD read_customizing.
  IF keys IS INITIAL.
    RAISE NEW...
  ENDIF.
    " do whatever needs doing
ENDMETHOD.

in the example it looks like a validation for input?

larshp avatar May 21 '22 06:05 larshp

In my opinion which option to choose depends on the current situation.

  • If an empty keys is a situation that should never happen I would also throw an exception.
  • If an empty keys is a valid option throwing an exception is an anti pattern. In this case the version with a return should be used.

ceedee666 avatar May 21 '22 06:05 ceedee666

Therefore the beginning of the paragraph should also be clarified.

There is no consensus on whether you should use CHECK or RETURN to exit a method if the input doesn't meet expectations.

ceedee666 avatar May 21 '22 06:05 ceedee666

@ceedee666 Either way, keeping a single IF statement in method is not a good practice. And I support @larshp using IF for raising the exception or using CHECK/RETURN statement and the placeholder for the logic when keys are not initial.

Sachinart avatar May 21 '22 07:05 Sachinart

Both examples below are valid, which one to use depends on the scenario and specific requirements. This part is not the matter of cleanliness.

METHOD read_customizing.
  IF keys IS INITIAL.
    RAISE NEW...
  ENDIF.
    " do whatever needs doing
ENDMETHOD.

or

METHOD read_document.
  IF keys IS INITIAL.
    RETURN.
  ENDIF.
    " do whatever needs doing
ENDMETHOD.

The main point is the whole method should never be wrapped in IF... ENDIF. That's anti-clean. :)

Jelena-P avatar May 23 '22 16:05 Jelena-P

If it really shouldn't happen (e.g. only through programmer error), then I'd even go as far as assert

There's another related (anti?)pattern:

if <someCondition>. 
  do stuff. 
  do things.
  ...
  more code.
  ...
else.
  raise exception ...
endif.

IMHO it is cleaner to invert the condition and do

if <notSomeCondition>. 
  raise exception ...
endif.
do stuff, party, whatever.

pokrakam avatar Jul 21 '22 19:07 pokrakam

If it really shouldn't happen (e.g. only through programmer error), then I'd even go as far as assert

There's another related (anti?)pattern:

if <someCondition>. 
  do stuff. 
  do things.
  ...
  more code.
  ...
else.
  raise exception ...
endif.

IMHO it is cleaner to invert the condition and do

if <notSomeCondition>. 
  raise exception ...
endif.
do stuff, party, whatever.

I agree with this approach!

ruthiel avatar Jul 22 '22 08:07 ruthiel

That's what I usually do too - put the shortest part of IF / exception first. Much easier to read IMHO.

Jelena-P avatar Jul 22 '22 16:07 Jelena-P

Closed with PR

rdibbern avatar Nov 28 '22 15:11 rdibbern

I think this issue is still relevant, and it is common practice to follow the discussion in issues instead of pull requests

larshp avatar Nov 28 '22 15:11 larshp

alternatively, I can follow the approach and roll back the changes in #277 with new changes?

larshp avatar Nov 28 '22 15:11 larshp