styleguides
styleguides copied to clipboard
Usage of Self-Reference 'me'
I have been using the self-reference 'me' to access instance private methods. I used 'me' to distinguish instance methods from static methods.
As per the clean code guidelines it's mentioned to omit it (cf. Omit the self-reference me when calling an instance method).
How would you suggest calling private static methods?
Well to distinguish between instance and static methods I use the class-name for the static methods. so just like this
method test.
" static method
zcl_class=>call_static_method( param ).
" instance method
call_instance_method( param ).
endmethod.
I don't distinguish here between public or private methods. In the method itself it doesn't matter if it is public or private.
Why do you want to distinguish at all?
class z_static_vs_instance definition
public
final
create public.
public section.
class-methods static_method.
methods instance_method.
methods calling_method.
protected section.
private section.
endclass.
class z_static_vs_instance implementation.
method instance_method.
endmethod.
method static_method.
endmethod.
method calling_method.
instance_method( ).
static_method( ).
endmethod.
endclass.
@thomham I think there is no real reason why you should / shouldn't add the class name in front of the method. It is only because I want to know, while reading the code if it is static or not. If "me" ( the current object ) affects the behavior of the method or not ( "affects" in terms of attributes - not parameters )
Omitting the name of the class let you refactor (rename) without effort. At least code within class. ( static methods and attributes )
Why do you want to distinguish at all?
class z_static_vs_instance definition public final create public. public section. class-methods static_method. methods instance_method. methods calling_method. protected section. private section. endclass. class z_static_vs_instance implementation. method instance_method. endmethod. method static_method. endmethod. method calling_method. instance_method( ). static_method( ). endmethod. endclass.
Could make life easier when reviewing or changing the code to see immediately if sth will affect the current instance or the class. To be honest, I'm with suynwa and use me. Normally my methods do not have instance or static in the name ;-)
The Clean ABAP book recommends against dropping static class references: “Make the fact that you’re calling a static method clear by always qualifying the method call with the class name.”
It suggests that an instance reference can cause confusion, which I can relate to from personal experience. To me it makes more sense: a static method call runs as a standalone piece of code in it's own bit of memory, you can almost think of it as another object instance.
But I guess a bigger question is why a static method is in use in first place? Instance methods are generally preferred as they are more testable if nothing else. Static methods should be used with care, they are good for factories and simple functions - but those are probably best off in their own class anyway.
So it should be very rare that a method calls its own class's static methods, and therefore it is a sensible convention (as used by the ABAP compiler) that everything without a reference refers to me
. Otherwise you'd theoretically also have to prefix all attributes with me->
and that gets really tedious to read. I only use it occasionally where I see potential for confusion.
Makes sense, your classes should mostly be comprised of instance methods. Static methods there perhaps belong to another class / concept.
If you have a Utility Class, i.e. a class that only has static methods, it might make sense not to qualify internal method calls with the class name, only because you know that all methods are static.
Just to make it clear: Normally I do not mix static and instance as well, I was just referring to the example. But anyway, I think it's helpful to use me to distinguish between global and local, not always, but also not forbidden.
Let's see which options we have:
- Use
me->
to call instance methods - Use the class name to call static methods
- Don't use qualifiers
Within an instance method
You've argued that you'd use me->
to call instance methods and not use the class name to call static methods. I'd argue for the other way around: use the class name to call static methods and don't use me->
to call instance methods. I feel that a static method call in an instance method is the odd one out, and I prefer to make that distinction clearer.
Within a static method
If, however, you're in a static method, you can't call any instance methods, and I think dropping the class name from a static method call would be OK, since it wouldn't be ambiguous.
My take then for your example:
method calling_method.
instance_method( ).
z_static_vs_instance=>static_method( ).
endmethod.
For the reference of future readers, i have since long stopped using the self-reference 'me' :)
Agree with @jordao76 , it's not that black&white. This static use case is obvious enough that it doesn't need any references:
CLASS zcl_widget_factory DEFINITION.
PUBLIC SECTION.
CLASS-METHODS get_blue_widget RETURNING VALUE(result) TYPE REF TO zcl_widget.
PRIVATE SECTION.
CLASS-METHODS get_widget RETURNING VALUE(result) TYPE REF TO zcl_widget.
ENDCLASS.
CLASS zcl_widget_factory IMPLEMENTATION.
METHODS get_blue_widget.
result = get_widget( ).
result->color = blue.
ENDMETHOD.
...
I don't use 'me' to access instance private methods, but I do use it to distinguish instance attributes from method parameters (even when there is no conflict). So I don't have to check the method signature or the class attributes when I skim through old code. Am I the only one?
I don't use 'me' to access instance private methods, but I do use it to distinguish instance attributes from method parameters (even when there is no conflict). So I don't have to check the method signature or the class attributes when I skim through old code. Am I the only one?
I always use me-> for attributes and methods from my classes or superclasses. Don't know if this is a good practice.
I always use me-> for attributes and methods from my classes or superclasses. Don't know if this is a good practice.
Not recommended in clean code. I think the original ABAP Guideline book recommended it back in 2009, but these days it really belongs in the same bucket as Hungarian notation. Use only where it really adds value, see link at the top of this issue.
I always use me-> for attributes and methods from my classes or superclasses. Don't know if this is a good practice.
You don't need to distinguish if you use ADT's context menu (F2/Alt+F2). Being the OP of this thread i must add that i personally do not use ME anymore.
I always use me-> for attributes and methods from my classes or superclasses. Don't know if this is a good practice.
Not recommended in clean code. I think the original ABAP Guideline book recommended it back in 2009, but these days it really belongs in the same bucket as Hungarian notation. Use only where it really adds value, see link at the top of this issue.
Thank you for your feedback. I'll correct this gap in my code!
Consensus here appears to be that the current recommendation in the guide to omit me->
where not syntactically required is correct.