code-pal-for-abap icon indicating copy to clipboard operation
code-pal-for-abap copied to clipboard

False Positive: Omit self-reference when calling instance method

Open zmsMarc opened this issue 4 years ago • 6 comments

Check Name Y_CHECK_SELF_REFERENCE

Actual Behavior check reacts to usage of instance attributes when calling an instance method

  DATA(sum) = aggregate_values( me->values ).

Expected Behavior Parameters should be irrelevant to the check

zmsMarc avatar Oct 12 '21 15:10 zmsMarc

The Clean ABAP discourages the use of self-reference. However, it is unclear if the rule is valid for instance methods and instance attributes, or methods only. In my understanding, we should avoid self-reference if it is not needed.

In your example, is there any local variable named values that could cause conflict?

lucasborin avatar Oct 13 '21 11:10 lucasborin

I am not sure why parameters should be irrelevant. That implies that these two statements should be treated differently:

result = some_method( me->value ). 
result = me->value.

They both should omit me-> unless there is a scope conflict.

pokrakam avatar Oct 13 '21 14:10 pokrakam

I interpreted Clean ABAP that its superfluous for method calls as they can only ever be on the instance, however there is no mention of attributes/variables. IMO it would be better to have it explicitly mention that we want to use an attribute of the class.

I assume the test would still mark my example as an error even when I explicitly want to use an attribute instead of a local variable with the same name?

zmsMarc avatar Oct 13 '21 14:10 zmsMarc

I assume the test would still mark my example as an error even when I explicitly want to use an attribute instead of a local variable with the same name?

No, it wouldn't. The Check should identify that you have a local variable with the same name (scope conflict) and exempt it automatically.

lucasborin avatar Oct 13 '21 14:10 lucasborin

Oh dear, looks like we submitted PRs in parallel 😆 Ah well, let the stylists pick one

pokrakam avatar Oct 13 '21 15:10 pokrakam

No, it wouldn't. The Check should identify that you have a local variable with the same name (scope conflict) and exempt it automatically.

I have been updating the unit test based on this thread, and I figured out the instance attribute is out of the Check's scope. If the Clean ABAP team accepts one of our pull requests, I will update the Check accordingly.

lucasborin avatar Oct 13 '21 19:10 lucasborin