code-pal-for-abap icon indicating copy to clipboard operation
code-pal-for-abap copied to clipboard

From Relational Operator to Predicate Function

Open lucasborin opened this issue 4 years ago • 3 comments

How about a new check to convert a relational operator to a predicate function?

‏Relational Operator Predicate Function
o1 CO o2 NOT contains_any_not_of( val = o1 sub = o2 )
o1 CN o2 contains_any_not_of( val = o1 sub = o2 )
o1 CA o2 contains_any_of( val = o1 sub = o2 )
o1 NA o2 NOT contains_any_of( val = o1 sub = o2 )
o1 CS o2 contains( val = to_upper( o1 ) sub = to_upper( o2 ) )
o1 NS o2 NOT contains( val = to_upper( o1 ) sub = to_upper( o2 ) )

Source: https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abencontains_functions.htm

Most of the non-ABAP developers do not understand the ABAP-specific relational operators.

lucasborin avatar Apr 05 '21 18:04 lucasborin

The operators CP and NP can be replaced by the predicate function contains and a suitable regular expression. The differences in case handling must also be respected here. Generally, a simple mapping like in CS and NS is not possible, but is also not usually required.

lucasborin avatar May 14 '21 19:05 lucasborin

Just my opinion, but I find the relational operators far shorter and readable, as is obvious from the list above. Yes, they're not common in other languages but are not hard to learn. Consider a real-world usage:

IF contains_any_of( val = sy-msgty sub = 'AEX' ). 
vs.
IF sy-msgty CA 'AEX'. 

String predicate functions are one of the few new ABAP features I have not fully embraced, I just find them awkward and only use them if a functional position is really required. If we want to compare to other languages, we can add JS to the example above:

IF sy-msgty CA 'AEX'.

if(msgty.match(/(A|E|X)/))

IF contains_any_of( val = sy-msgty sub = 'AEX' ).

I am not aware this is in the style guides either. As I said this is just my opinion though, perhaps some may find it useful but I would likely disable this check. I just can't see any benefit to limiting the use of relational operators.

pokrakam avatar May 15 '21 06:05 pokrakam

Let's start a thread in the Clean ABAP.

lucasborin avatar Aug 13 '21 18:08 lucasborin