传递参数与从函数返回参数 [英] Passing a parameter versus returning it from function

查看:45
本文介绍了传递参数与从函数返回参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从标题中可以清楚地看出我们应该更喜欢哪种方法?

As it might be clear from the title which approach should we prefer?

意图是传递一些方法参数并得到一些东西作为输出.我们可以传递另一个参数,方法会更新它,方法现在不需要返回任何东西,方法只会更新输出变量并将其反映给调用者.

Intention is to pass a few method parameters and get something as output. We can pass another parameter and method will update it and method need not to return anything now, method will just update output variable and it will be reflected to the caller.

我只是想通过这个例子来构建问题.

I am just trying to frame the question through this example.

List<String> result = new ArrayList<String>();

for (int i = 0; i < SOME_NUMBER_N; i++) {
    fun(SOME_COLLECTION.get(i), result);
}

// in some other class
public void fun(String s, List<String> result) {
    // populates result
}

对比

List<String> result = new ArrayList<String>();

for (int i = 0; i < SOME_NUMBER_N; i++) {
    List<String> subResult = fun(SOME_COLLECTION.get(i));
    // merges subResult into result
    mergeLists(result, subResult);
}

// in some other class
public List<String> fun(String s) {
    List<String> res = new ArrayList<String>();
    // some processing to populate res
    return res;
}

我知道一个通​​过引用,另一个没有.

I understand that one passes the reference and another doesn't.

我们应该选择哪一种(在不同情况下)以及为什么?

Which one should we prefer (in different situations) and why?

更新:只考虑可变对象.

推荐答案

从函数返回值通常是一种更简洁的代码编写方式.由于创建和销毁指针的性质,传递和修改值更像是 C/C++ 风格.

Returning a value from the function is generally a cleaner way of writing code. Passing a value and modifying it is more C/C++ style due to the nature of creating and destroying pointers.

开发人员通常不希望通过将其传递给函数来修改他们的值,除非该函数明确声明它修改了值(无论如何我们通常会略读文档).

Developers generally don't expect that their values will be modified by passing it through a function, unless the function explicitly states it modifies the value (and we often skim documentation anyway).

不过也有例外.

考虑 Collections.sort 的例子,它实际上做了一个列表的就地排序.想象一个包含 100 万个项目的列表,您正在对其进行排序.也许您不想创建包含另外 100 万个条目的第二个列表(即使这些条目指向原始条目).

Consider the example of Collections.sort, which does actually do an in place sort of a list. Imagine a list of 1 million items and you are sorting that. Maybe you don't want to create a second list that has another 1 million entries (even though these entries are pointing back to the original).

支持不可变对象也是一种很好的做法.不可变对象在开发的大多数方面(例如线程)引起的问题要少得多.因此,通过返回一个新对象,您并没有强制参数可变.

It is also good practice to favor having immutable objects. Immutable objects cause far fewer problems in most aspects of development (such as threading). So by returning a new object, you are not forcing the parameter to be mutable.

重要的部分是明确您在方法中的意图.我的建议是尽可能避免修改参数,因为它不是 Java 中最典型的行为.

The important part is to be clear about your intentions in the methods. My recommendation is to avoid modifying the parameter when possible since it not the most typical behavior in Java.

这篇关于传递参数与从函数返回参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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