Is it a good practice to change arguments in Java

拈花ヽ惹草 提交于 2019-11-27 23:53:11

问题


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

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

Is it considered good or bad practice to change arguments of methods in Java?


回答1:


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.




回答2:


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).




回答3:


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.




回答4:


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.




回答5:


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.




回答6:


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).



来源:https://stackoverflow.com/questions/12019978/is-it-a-good-practice-to-change-arguments-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!