Passing a parameter and returning it from a function

As the name implies, which approach should we prefer?

The intention is to pass a few parameters to the method and get something as an output. We can pass another parameter, and the method will update it, and the method does not need to return anything, the method will simply update the output variable, and it will be reflected to the caller.

I'm just trying to ask a question in 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 } 

against

 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 link and the other does not.

Who should we prefer (in different situations) and why?

Refresh . Keep this in mind only for mutable objects.

+6
source share
5 answers

Returning a value from a function is usually a cleaner way of writing code. Passing a value and changing it is more of a C / C ++ style because of the nature of the creation and destruction of pointers.

Developers usually do not expect their values ​​to be changed by passing them through a function, unless the function explicitly states that it changes the value (and we often refuse documentation).

However, there are exceptions.

Consider the example Collections.sort , which actually makes the list look like. Imagine a list of 1 million items and you sort it. Perhaps you do not want to create a second list with another 1 million entries (even if these entries point to the original).

It is also recommended that you stick with immutable objects. Immutable objects cause far fewer problems in most aspects of development (such as threads). Thus, by returning a new object, you do not force the parameter to be mutable.

The important part is to clearly understand your intentions in the methods. My recommendation is to avoid modifying the parameter whenever possible, as this is not the most common behavior in Java.

+14
source

You must return it. The second example you provided is the path.

First of all, it is more clear. When other people read your code, there is no information that they may not notice that the parameter is changing as output. You can try to name the variables, but when it comes to code readability, this is preferable.

The reason BIG is why you should return it, and not pass it, with immutable objects. Your example, List, is modified, so it works fine. But if you try to use String in this way, it will not work.

Since strings are immutable if you pass the string as a parameter, then the function should say:

 public void fun(String result){ result = "new string"; } 

The value of the result you passed in will not be changed. Instead, the local variable "result" now points to a new line inside fun, but the result of your calling method still points to the original line.

If you called:

 String test = "test"; fun(test); System.out.println(test); 

It will print: "test", not "new line"!

So definitely, he surpasses the comeback. :)

+6
source

Returning, it will keep your code cleaner and reduce the connection between methods / classes.

0
source

This is more about best practices and your own programming method. I would say if you know that this will be one return value return function, for example:

function IsThisNumberAPrimeNumber {}

Then you know that it will ever return a boolean value. I usually use functions as helper programs, not as large routines. I also apply naming conventions that help dictate what I expect the sub \ function will return. Examples:

GetUserDetailsRecords GetUsersEmailAddress IsEmailRegistered

If you look at these 3 names, you can say that the first one will give you a list or a class of several detailed user entries, the second will give you a string email value, and the third will most likely give you a boolean value. If you change the name, you change the value, so I would say that we consider this additionally.

0
source

I do not think that we understand that these are two completely different types of actions. Passing a variable to a function is a means of providing data to the function. Return from function - a method of transferring data from a function.

If you mean the difference between these two actions:

 public void doStuff(int change) { change = change * 2; } 

and

 public void doStuff() { int change = changeStorage.acquireChange(); change = change * 2; } 

Then the second, as a rule, is cleaner, but there are several reasons (security, visibilty function, etc.) that may prevent you from transmitting data in this way.

It is also preferable because it simplifies code reuse and also makes it more modular.

0
source

Source: https://habr.com/ru/post/914669/


All Articles