Skip to content

[java] Deprecate UseObjectForClearerAPI #3741

Closed

Description

@oowekyala

The rule UseObjectForClearerAPI reports methods that take more than 3 String parameters. I think the justification is ok, but it's bizarre to single out strings. OTOH, generalizing this to any type of argument just gives ExcessiveParameterList. Although it's less thoroughly justified, ExcessiveParameterList also recommends using an object to wrap parameters.

Another problem with UseObjectForClearerAPI is that it doesn't care about constructors. Clearly,

callFun("a", "b", "c", "d")

is no less readable than

callFun(new FunArguments("a", "b", "c", "d"))

So to stay true to its goal of increasing readability, the rule should flag constructors, and also not simply recommend using an object, but also maybe the builder pattern. Since this is burned into the rule name we can't really change that (the rule name doesn't match our guidelines).


I think UseObjectForClearerAPI should be deprecated and its description used to enrich the description of ExcessiveParameterList.

Activity

  1. added
    an:enhancement
    on Jan 15, 2022
  2. added
    is:deprecation
    and removed
    an:enhancement
    on Jan 17, 2022
  3. added this to the 7.x milestone on Jan 23, 2023
  4. zman0900 commented on Apr 24, 2025

    @zman0900

    +1 This rule also doesn't seem to account for overriding of library / JDK methods. For example, rule is violated by overriding org.xml.sax.ext.DefaultHandler2#resolveEntity(java.lang.String, java.lang.String, java.lang.String, java.lang.String) when implementing a custom handler.

  5. modified the milestones: 7.x, 7.26.0 on Jun 23, 2026
  6. added a commit that references this issue on Jun 23, 2026
  7. added a commit that references this issue on Jun 23, 2026
    404920b
  8. jtotht commented on Jul 7, 2026

    @jtotht

    I just noticed this in the release notes.

    [I]t's bizarre to single out strings.

    I think it did make sense to single out strings – one may be tempted to use strings for everything (especially if the data comes from an untyped source like a properties file or a servlet request), which highly increases the risk of swapping parameters without causing compile-time errors. Consider the following signature:

    void banUser(String uuid, String duration, String reason, String notify);
    
    banUser("00000000-0000-0000-0000-000000000000", "bad behavior", "1 day", "true"); // Whoops, duration and reason swapped, but the compiler doesn’t notice this

    with the following one:

    void banUser(java.util.UUID uuid, java.time.Duration duration, String reason, boolean notify);
    
    banUser(new UUID(0, 0), "bad behavior", Duration.ofDays(1), true); // Compilation failure due to the signature mismatch

    Four parameters are not that excessive that I’d want to configure ExcessiveParameterList to report that regardless of the type, but four strings in a row are confusing and should be avoided if possible.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    is:deprecation

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions