GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] Document Reasons to Avoid Reassigning Parameters · Issue #2219 · pmd/pmd

[java] Document Reasons to Avoid Reassigning Parameters #2219

New issue
Copy link
New issue
Copy link
Closed
#3255
Closed
[java] Document Reasons to Avoid Reassigning Parameters#2219
#3255
Copy link
Labels
in:documentationAffects the documentation [doc]
Milestone
6.35.0

Description

@numeralnathan
numeralnathan
opened on Jan 14, 2020
Issue body actions

Affects PMD Version:

6.20.0

Rule:

Java Best Practices - AvoidReassigningParameters
https://pmd.github.io/latest/pmd_rules_java_bestpractices.html#avoidreassigningparameters

Description:

The comment for this rule is:

Reassigning values to incoming parameters is not recommended. Use temporary local variables instead.

This comment does not tell me why reassigning parameters is "not recommended". Please add reasons why this is a bad idea so that users can make better informed decisions about the rule.

I tried looking for reasons on the Internet. One reason was due to parameters being passed via reference as in C/C++. This is not applicable to Java since the method parameter reference can be changed without impacting the caller. Here is another reason:

Multiple assignments of different values to a single parameter make it difficult for you to know what data should be contained in the parameter at any particular point in time. The problem worsens if your parameter and its contents are documented but the actual value is capable of differing from what’s expected inside the method.

Please find other reasons for this rule and add it to the documentation.

Reacted by Linus Fernandes and navrkald

Activity

  1. numeralnathan commented on Jan 14, 2020

    @numeralnathan
    numeralnathan
    on Jan 14, 2020
    Author
    More actions

    Here is another reason but I find it lacking.

    By some, it is considered an ill approach to assign values to parameters within a method's body, as it is, at times, confusing. Some developers prefer always assuming that a parameter is never assigned any value during a method's run.

    Reacted by Linus Fernandes
  2. linusjf commented on Jan 15, 2020

    @linusjf
    linusjf
    on Jan 15, 2020
    More actions

    Here's another:
    https://softwareengineering.stackexchange.com/a/245809/205864

  3. adangel commented on Jan 16, 2020

    @adangel
    adangel
    on Jan 16, 2020
    Member
    More actions

    Yes, this rule clearly needs a better explanation. As you pointed out, this is not a technical issue (like pass-by-reference), but more a case of clean code - hence it is in the category "Best Practices".

    I found one IMHO good explanation here: https://stackoverflow.com/a/2229437/1169968

    Assigning a method parameter is not something most people expect to happen in most methods.
    Since we read the code with the assumption that parameter values are fixed, an assignment is
    usually considered poor practice, if only by convention and the principle of least astonishment.

    There are always alternatives to assigning method parameters: usually a local temporary copy is
    just fine. But generally, if you find you need to control the logic of your function through parameter
    reassignment, it could benefit from refactoring into smaller methods.

    Reassigning parameters in a very short method is probably no problem. But if the reassignment and usage is many lines apart, then you might not be aware of this.
    Another example is: If you know how the method you are reading should be implemented, and you e.g. know, that the algorithm needs to convert the input value before using it (like sanitizing input) and this happens too many lines apart, you might want to scroll up again to double check, this sanitizing really happened. In that case, it's better to introduce a new local variable with a different name (in this example e.g. "sanitizedInput").

    It actually might indicate a programming error, like the example here. In Java, you can't return values from method calls by (re)assigning parameters.
    From https://stackoverflow.com/a/2229479/1169968:

    Reassigning to the method parameter variable is usually a mistake if the parameter is a reference
    type.

    Consider the following code:

    MyObject myObject = new myObject();
    myObject.Foo = "foo";
    doFoo(myObject);
    // what's the value of myObject.Foo here?
    public void doFoo(MyObject myFoo){   
      myFoo = new MyObject("Bar");
    }

    Many people will expect that at after the call to doFoo, myObject.Foo will equal "Bar". Of course, it
    won't - because Java is not pass by reference, but pass by reference value - that is to say, a copy of
    the reference is passed to the method. Reassigning to that copy only has an effect in the local
    scope, and not at the callsite. This is one of the most commonly misunderstood concepts.

  4. linusjf commented on Jan 17, 2020

    @linusjf
    linusjf
    on Jan 17, 2020 via email
    More actions
    What the StackOverflow post describes is an understanding error, that may cause a programming error. Fine enough. I think the OP meant is that if the understanding is not flawed, reassigning a parameter in the method (locally) will not have any side effect on the original reference in the calling method since the original reference is never lost. I don't think too many people gave much thought to the semantics behind this rule. I know I didn't until now.
  5. adangel
    added
    in:documentationAffects the documentation [doc]
    on Jan 17, 2020
  6. adangel
    added this to the 6.35.0 milestone on May 13, 2021
  7. adangel
    added a commit that references this issue on May 13, 2021

    [doc] AvoidReassigningParameters - improve rule doc

    This issue will close once commit 7c2fffb is merged into the 'main' branch.
    7c2fffb
  8. oowekyala
    closed this as completedin 7c2fffbon May 14, 2021
  9. oowekyala
    closed this as completedin #3255on May 14, 2021
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

    in:documentationAffects the documentation [doc]

    Type

    No type

    Projects

    No projects

      Milestone

      • 6.35.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.