Skip to content

Feature request: Remove unnecessary CLEAR after variable definition #61

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
ConjuringCoffee opened this issue Jun 13, 2023 · 4 comments
Closed
Assignees

Comments

@ConjuringCoffee
Copy link
Contributor

Hi Jörg-Michael, I'd like to request an action to remove unnecessary CLEAR statements after a variable has been defined. There is no reason why you should clear a variable you just defined if you're not in some kind of loop.

Example:

METHOD example.
  DATA lv_string type string.
  CLEAR lv_string.
  " [...]
ENDMETHOD.

Expected result:

METHOD example.
  DATA lv_string type string.
  " [...]
ENDMETHOD.
@ConjuringCoffee ConjuringCoffee changed the title Feature request: Remove CLEAR after variable definition Feature request: Remove unnecessary CLEAR after variable definition Jun 13, 2023
@jmgrassau
Copy link
Member

Hi ConjuringCoffee,

that makes sense, although I think I haven't seen too many of those, at least with CLEAR being right after the declarations.

And I assume you don't mean CLEAR further down the method? That's also unnecessary sometimes, but could be intended as a form of safeguarding in the sense of "from here we need to start with an initial value, whatever we may do with this variable in the above blocks (possibly at a later point in time)".

However, what might be included (optionally) in such a rule is removing CLEAR rv_result if ABAP cleaner can "see" the method signature RETURNING VALUE(rv_result) TYPE ....

Kind regards,
Jörg-Michael

@ConjuringCoffee
Copy link
Contributor Author

although I think I haven't seen too many of those, at least with CLEAR being right after the declarations.

I unfortunately see this quite often in legacy code. 😅
It also happens accidentally when encapsulating the logic from inside a LOOP: If the variables were cleared at the start of the loop before, then they are cleared at the start of the method afterwards. This has to be removed manually then.

And I assume you don't mean CLEAR further down the method?

I agree, it would be better to only apply the action to CLEARs that come directly after the declarations.

@MDagni
Copy link

MDagni commented Sep 26, 2023

I unfortunately see this quite often in legacy code. 😅

I see this a lot too. I think people just want to feel safe, or don't know that local variables are always empty at the beginning even if the method/form is called multiple times.

I also see if sy-subrc = 0. as the first statement inside a select-endselect loop, so don't underestimate the power of feeling safe 😃

@jmgrassau jmgrassau self-assigned this Oct 25, 2023
jmgrassau added a commit to jmgrassau/abap-cleaner that referenced this issue Oct 29, 2023
@jmgrassau
Copy link
Member

Hi ConjuringCoffee and Mehmet,

as mentioned in more detail in #111, this new cleanup rule "Remove needless CLEAR" is now available with version 1.9.0! In my sample code, I indeed found quite a number of places with such CLEAR statements esp. at method start. In one case, there were approx. 20 DATA lines, followed by approx. 10 CLEARs of the variables that were just declared!

And yes, with respect to method start, the rule only deletes (or adds a TODO comment for) CLEAR of local variables that is found before any other executable statement, except for ASSERT (actually I forgot BREAK-POINT and LOG-POINT statements, those are acceptable before a to-be-removed CLEAR as well – will add that with the next release).

Kind regards,
Jörg-Michael

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

3 participants