SAP / styleguides

This repository provides SAP style guides for coding and coding-related topics.
Other
1.68k stars 446 forks source link

Accessing TYPES through instance variables #288

Closed larshp closed 1 year ago

larshp commented 2 years ago

For a section in the guide suggest adding "Don't access TYPES through instance variables"

Background: same story as https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#dont-call-static-methods-through-instance-variables

anti pattern example,

CLASS lcl DEFINITION.
  PUBLIC SECTION.
    TYPES foo TYPE i.
ENDCLASS.
CLASS lcl IMPLEMENTATION.
ENDCLASS.

INTERFACE lif.
  DATA ref TYPE REF TO lcl.
  TYPES blah TYPE ref->foo.   " <- this is bad
ENDINTERFACE.

instead do,

CLASS lcl DEFINITION.
  PUBLIC SECTION.
    TYPES foo TYPE i.
ENDCLASS.
CLASS lcl IMPLEMENTATION.
ENDCLASS.

INTERFACE lif.
  DATA ref TYPE REF TO lcl.
  TYPES blah TYPE lcl=>foo.   " <- this is good
ENDINTERFACE.

any thoughts/comments?

nununo commented 2 years ago

Agree!

N2oB6n-SAP commented 1 year ago

I agree, even though (this holds true for the referenced section on instance variables as well)

[...] is a potential source of confusion [...]

feels like a rather soft and/or incomplete argument to me.

I would prefer to actually name the confusion (the accessed type not being instance-specific/dependent) and possibly argue that refactoring of the instance variable [name] would misleadingly touch a piece of code that is unrelated to the type definition.

@larshp Would you like to open a PR yourself?

bjoern-jueliger-sap commented 1 year ago

Merged with #305