styleguides icon indicating copy to clipboard operation
styleguides copied to clipboard

Misleading Method Name vs Built-in Function

Open lucasborin opened this issue 3 years ago • 4 comments

In a short dialogue about the self-reference rule (Code Pal, Clean ABAP), it came to a strange scenario where a class method is named as a built-in function.

For instance:

CLASS person DEFINITION.
  PUBLIC SECTION.
    METHODS is_valid RETURNING VALUE(result) TYPE abap_bool.
  PROTECTED SECTION.
    METHODS strlen RETURNING VALUE(result) TYPE i.    
  PRIVATE SECTION.
    DATA name TYPE string.
ENDCLASS.

If you want to call the built-in function strlen, you are not allowed anymore because you "replaced" it:

  METHOD is_valid.
    result = xsdbool( strlen( name ) > 1 ).  "<---- syntax error
  ENDMETHOD.

But, you can:

  METHOD is_valid.
    result = xsdbool( strlen( ) > 1 ).
    result = xsdbool( me->strlen( ) > 1 ).
  ENDMETHOD.

I haven't found anything on this matter in the style guide, and I will open a pull request discouraging the reuse of the built-in function names like the line_exists, lines, line_index, strlen, etc. In parallel, I would like to see your opinion about it.

Do you consider it a bad practice? Do you see it needed sometimes?

lucasborin avatar Oct 14 '21 14:10 lucasborin

image

Ref: Obscuring with Methods

lucasborin avatar Oct 14 '21 14:10 lucasborin

This is all well and good, but does the compiler or code pal warn you that you're obscuring a built-in function? If not, you should be aware of all existing built-in functions.

There's also the problem of future built-in functions. Should be rare, but we should not retroactively warn users about new built-in functions introduced in the language. In the end, if the class is well designed and tested and uses a method with the name of a built-in function, then it was not intended to use the built-in function itself and all should be good.

jordao76 avatar May 22 '22 00:05 jordao76

here, https://rules.abaplint.org/method_overwrites_builtin/

larshp avatar May 22 '22 15:05 larshp

Can't this issue be closed? #251 from 2022 covered this matter sufficiently in my opinion.

ConjuringCoffee avatar Mar 14 '23 13:03 ConjuringCoffee