styleguides
styleguides copied to clipboard
New rule: prefer inferring types
Proposed Rule Prefer inferring types where possible, instead of specifying the same type twice.
Example:
CLASS lcl DEFINITION.
PUBLIC SECTION.
CLASS-METHODS foo IMPORTING val TYPE string.
ENDCLASS.
CLASS lcl IMPLEMENTATION.
METHOD foo.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
lcl=>foo( CONV string( 2 ) ).
In this example lcl=>foo( CONV string( 2 ) ).
can be replaced with lcl=>foo( CONV #( 2 ) ).
, making it easier to change the type of "val". Note, if the type is changed, the compiler will give a syntax error(for most cases?)
While I generally agree that #
-inference should be used in most cases I am torn about your particular example:
When it comes to CONV
I would personally favor the explicit repetition of string
. ABAP's conversion from, to, and between char-like types is notorious for causing headache or even subtle bugs. I dislike the fact that in this instance there is program logic at work on the consumer side (CONV string( ... )
does serious work, has implications and special cases) which would change its behavior if the API (unexpectedly and contrary to the best practice of stable public interfaces) changed its types.
Besides this caveat, however, I vote in favor of such a rule. In all places where type inference would work (and is not tied to inferred conversion logic) noting down the type is noise that can be avoided. The reader must be able to understand that "what" of a given piece of code without precise type information.