Skip to content

Feature request: Handle unused method parameters #224

Closed

Description

@ConjuringCoffee

Hi Jörg-Michael, I would like to request another feature. I realize that some of my previous issues didn't get an answer yet, but I would like to try nonetheless. 😉

The rule "Delete unused variables" currently only handles local variables. Although I don't use it for variable deletion, I highly value the functionality to add TODO comments for unused local variables. I would like to see this functionality extended to method parameters. For global classes, this should be well within the limitations of the ABAP Cleaner because the method definition and the method implementation is in the same include.

Example:

CLASS ycl_example_2 DEFINITION
  PUBLIC
  INHERITING FROM ycl_example_3
  FINAL
  CREATE PUBLIC.

  PUBLIC SECTION.
    METHODS example
      IMPORTING
        iv_i TYPE i.
ENDCLASS.


CLASS ycl_example_2 IMPLEMENTATION.
  METHOD example.
    RETURN.
  ENDMETHOD.
ENDCLASS.

Possible result:

  PUBLIC SECTION.
    METHODS example
      IMPORTING
        " TODO: variable is never used (ABAP cleaner)
        iv_i TYPE i.

I don't know if it is problematic to add regular comments within the method definition though. Another possibility would be to add the TODO comment at the start of the method. This would show the TODO comment in the SAP GUI as well, but it would require more details in the comment (and probably more logic to keep the comment updated).

  METHOD example.
    " TODO: variable IV_I is never used (ABAP cleaner)
    RETURN.
  ENDMETHOD.

What do you think? 🙂

Activity

  1. jmgrassau commented on Jan 2, 2024

    @jmgrassau
    Member

    Hi ConjuringCoffee,

    I do like the idea, and in principle (after some experimenting) it would almost work out of the box as part of the "Delete unused variables" rule (only if the method signature is in sight, of course).

    We could nicely differentiate expectations, e.g. add a comment if

    • an IMPORTING parameter is never used,
    • a CHANGING parameter is never used or assigned,
    • an EXPORTING or RETURNING parameter is never assigned or cleared.

    Maybe it would make sense to (optionally) restrict this to private methods only, because with a public or protected method, parameters may not necessarily be needed in every implementation, but possibly only in a child class or in a different class that implements the same interface. On the other hand, you could then add ##NEEDED to the signature to suppress the TODO comment – this also satisfies the Extended Check (SLIN) for unused parameters.

    Some care would then indeed be required to avoid duplicate TODOs, remove obsolete TODOs etc.

    Kind regards,
    Jörg-Michael

  2. self-assigned this
    on Jan 4, 2024
  3. jmgrassau commented on Jan 4, 2024

    @jmgrassau
    Member

    Hi ConjuringCoffee,

    here's what my attempts look like so far:

    image

    The selection for the options always looks like this …

    image

    … the idea being that interface methods may be implemented in many different classes and possibly provide IMPORTING or CHANGING parameters that are required in some, but not all implementations (so not using them may be fine). By contrast, EXPORTING parameters should always at least be CLEARed (unless they are VALUE parameters). RETURNING parameters are initialized by default, and sometimes a method simply keeps them initial in every case (while a child class may return something different), so again, it would be fine to not assign them.

    Examples:

    image

    Obsolete TODO comments are automatically removed (e.g. if parameters are now used or assigned, were renamed, removed, etc.).

    Kind regards,
    Jörg-Michael

  4. ConjuringCoffee commented on Jan 5, 2024

    @ConjuringCoffee
    ContributorAuthor

    That sounds really good! 👍

  5. jmgrassau commented on Jan 5, 2024

    @jmgrassau
    Member

    Hi ConjuringCoffee,

    thanks again for this idea! The new cleanup rule is now available in version 1.13.0, which was just released!

    Kind regards,
    Jörg-Michael

  6. ConjuringCoffee commented on Jan 8, 2024

    @ConjuringCoffee
    ContributorAuthor

    Awesome, thank you. I think this will really help us improve our code 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions