Java为参数分配新值,这被认为是一种不好的做法吗? [英] Java assigning a new value to a parameter, is this considered a bad practice?

查看:88
本文介绍了Java为参数分配新值,这被认为是一种不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里已经阅读了这个问题:为方法参数指定新值是否有问题?。但是我不清楚是否做了类似的事情:

I have read the question here: Is it problematic to assign a new value to a method parameter?. However it is not clear to me if doing something like:

public void myMethod(Object obj) {
    doSomething(obj);
    obj = getNewObj();
}

或:

public void anotherMethod(Object obj) {
     obj = doSomething(obj):
}

这基本上只是为了避免声明一个新的局部变量,这值得吗?这被认为是一种不好的做法吗?

This is basically just to avoid declaring a new local variable, is this worth it?, is this seen as a bad practice?.

推荐答案

这是一种不好的做法。您将很难找到牺牲可读性值得的场景。对于那些不了解Java的按价值传递政策的人来说,这一点尤其令人困惑,遗憾的是很多人。

This is a bad practice. You will be hard pressed to find a scenario where the sacrificed readability is worth it. This will be especially confusing to anyone who doesn't understand Java's "pass by value" policy, which sadly is a lot of people.

这篇关于Java为参数分配新值,这被认为是一种不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆