Skip to Content
0
Former Member
Jul 04, 2014 at 01:35 PM

Proper way of changing attributes by class' own methods

63 Views

Dear moderators,

this is a generic question which is independent of http://scn.sap.com/thread/3582195, so PLEASE keep this open.

Hello everybody,

I am not sure what is the most appropriate/robust way to change attributes of a class by the class' own private methods - just change them or use a parameter? Simple example:

CLASS sample_class DEFINITION.

PUBLIC SECTION.

METHODS main.

PRIVATE SECTION

DATA attribute TYPE c.

METHODS:

meth1,

meth2 CHANGING ch_attribute TYPE c.

...

ENDCLASS.

CLASS sample_class IMPLEMENTATION.

METHOD main.

me->meth1.

me->meth2( attribute ).

METHOD meth1.

me->attribute = 'X'.

ENDMETHOD.

METHOD meth2.

ch_attribute = 'X'.

ENDMETHOD.

ENDCLASS.

Would you prefer meth1 or meth2 or does one of the solutions even raise problems that I don't see? Of course, if a method changes lots of attributes it is impractical to use parameters, but besides that?

created by Tomas Buryanek in ABAP Development


Difference would be third method:

set_attribute IMPORTING i_value TYPE c.

...

METHOD set_attribute.

me->attribute = i_value.

ENDMETHOD.

...

lo=>set_attribute( 'A' ).

which is example of classic "setter" method.

For this topic check theory and usage of encapsulation OOP principle. It will help understand 😊