Rule Definition
The ON CHANGE OF statement can prove dangerous and is often misunderstood. Also, troubleshooting problems created by inadequate use of ON CHANGE OF statement can often be time-consuming because such errors are very difficult to reproduce and often appear to be random ones. Indeed, the use of the ON CHANGE OF statement is not suitable within functions or subroutines as many developers expect the comparison field's value to be reset for every new function call which is not the case.
The control structure ON CHANGE OF - ENDON is not allowed in ABAP Objects, i.e. in a method.
Remediation
You can replace the ON CHANGE OF statements by explicitly programming the comparisons with IF statements.
Violation Code Sample
LOOP AT ts_invoices INTO wa_invoices
ON CHANGE OF wa_invoices-matnr.
...
ENDON.
ENDLOOP.
Fixed Code Sample
Remediation example:
LOOP AT ts_invoices INTO wa_invoices
IF wa_invoices-matnr NE ltp_matnr_old.
Ltp_matnr_old = wa_invoices-matnr.
...
ENDIF.
ENDLOOP.
Reference
Enhancing the Quality of ABAP Development, Meijs-Krouwels-Heulmans-Sommen, SAP Press
Page 98
Related Technologies
Technical Criterion
Programming Practices - Unexpected Behavior
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.