IF conditions should be simple to read
2021-11-01
Because…why should they be complex to read? It would only make it harder to maintain in the future.
Just because an IF condition is complex doesn’t mean it has to be complicated.
Clean ABAP has some nice suggestions on how to achieve this:
Here’s an example of a complex If condition:
IF price > 1000 AND customer_debt > 10000 AND salesorder_status = '1'.
" code to block invoice
ENDIF.
Ok, I concede that it’s not that complex, but it will serve as example.
This would be an improvement:
IF should_be_blocked( ).
" code to block invoice
ENDIF.
METHOD should_be_blocked.
result = COND #(
WHEN is_expensive( price ) AND
customer( kunnr ).is_deadbeat( ) AND
NOT salesorder( vbeln ).is_delivered( ) THEN abap_true
ELSE abap_false ).
ENDMETHOD.
(...)
Yes, this is clearly overkill. But I took it too far on purpose, just to exemplify what can be done to make the IF condition simpler to read and understand.
Greetings from Abapinho.