Is it a good practice to change arguments in Java

前端 未结 6 774
小蘑菇
小蘑菇 2020-12-16 17:54

Suppose I am writing a method foo(int i) in Java.
Since i is passed by value it is safe to change it in foo. For example



        
相关标签:
6条回答
  • 2020-12-16 18:33

    It's merely a personal opinion, but I think it can be confusing for other people that may want to use the original parameter value later in that code, and may not notice that it has already been changed.

    Plus, it's cheap to simply create another variable and assign it the modified value (i.e., int j = i + 1).

    0 讨论(0)
  • 2020-12-16 18:41

    It's considered bad practice in general, though some people overlook it as you can see in the other answers.

    For parameters like primitives that are directly passed in by value, there is no advantage in overriding the original variable. In this case you should make a copy as suggested by @João.

    For parameters whose reference is passed in by value (objects), if you modify the handle to point to a different object, that is downright confusing. It's all the more important because modifying the contents of an object passed as a parameter will modify the original object too.

    If you replace the object referred to by the handle, and then modify its contents, the object referred to by the original reference in the caller will not be replaced, but someone reading the code might expect it to be.

    Whereas if you don't replace the object, and modify the contents, the method calling your method might not expect this change. This category generally comes under security-related bad practices.

    0 讨论(0)
  • 2020-12-16 18:44

    Since i is passed by value it is safe to change it foo()

    It is absolutely safe even when you pass an object reference because they are local: i.e., assigning a new reference to the local reference will not have any impact on the original reference in the calling code

    It's your personal choice. However i would not change the argument values as one might loose the track of the actual value passed in to this method.

    0 讨论(0)
  • 2020-12-16 18:47

    Depends on context. I lean towards "bad practice", for two reasons:

    1. Some people may think the original value is being changed.
    2. It may make the code harder to reason about (mitigated with appropriately-short methods).

    A third issue pops up when it's a reference value. If you modify the parameter reference to point at something else and change its state, the original won't be modified–this may or may not be what's intended. If you create another reference to the parameter and change the new reference's state, the parameter's reference will be changed–which also may or may not be what's intended.

    0 讨论(0)
  • 2020-12-16 18:53

    What is important to note is that i = i + 1; does not really change i. It only changes your local copy of i (in other words, the i in the calling code won't change).

    Based on that, it is a matter of readability and avoiding unexpected behaviour in your code by complying with the POLS (Principle Of Least Surprise).

    0 讨论(0)
  • 2020-12-16 18:58

    Neutral. But it would be considered a better practice by many people to change the method to:

    void foo(final int i) {
        int j = i + 1; // not change i
        ...
    }
    

    Feel free to work either way.

    0 讨论(0)
提交回复
热议问题