Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should an API return 'void'?

Tags:

When writing an API or reusable object, is there any technical reason why all method calls that return 'void' shouldn't just return 'this' (*this in C++)?

For example, using the string class, we can do this kind of thing:

string input= ...; string.Join(input.TrimStart().TrimEnd().Split("|"), "-"); 

but we can't do this:

string.Join(input.TrimStart().TrimEnd().Split("|").Reverse(), "-"); 

..because Array.Reverse() returns void.

There are many other examples where an API has lots of void-returning operations, so code ends up looking like:

api.Method1(); api.Method2(); api.Method3(); 

..but it would be perfectly possible to write:

api.Method1().Method2().Method3() 

..if the API designer had allowed this.

Is there a technical reason for following this route? Or is it just a style thing, to indicate mutability/new object?

(x-ref Stylistic question concerning returning void)


EPILOGUE

I've accepted Luvieere's answer as I think this best represents the intention/design, but it seems there are popular API examples out there that deviate from this :

In C++ cout << setprecision(..) << number << setwidth(..) << othernumber; seems to alter the cout object in order to modify the next datum inserted.

In .NET, Stack.Pop() and Queue.Dequeue() both return an item but change the collection too.

Props to ChrisW and others for getting detailed on the actual performance costs.

like image 236
JBRWilkinson Avatar asked Jun 28 '10 16:06

JBRWilkinson


People also ask

What does returning void mean?

Void as a Function Return Type Void functions, also called nonvalue-returning functions, are used just like value-returning functions except void return types do not return a value when the function is executed. The void function accomplishes its task and then returns control to the caller.

When would you use a void method?

The most common use of void method in java is when we want to change the internal state of the object but do not require the updated state. For example in VoidWithReturnExample. java , the method setName and setAge are only used to change the name and age respectively, but they don't return anything.

Should I use void functions?

You should use void. For then it highlights the fact the the method in question has side-effects, and it was designed to have side-effects. This does two things, First, it tells the reader that all non-void methods don't change state, so one can be sure that calling this method wont have any un-intended side-effects.

Can we use return in a void method yes or no?

Any method declared void doesn't return a value. It does not need to contain a return statement, but it may do so.


2 Answers

Methods that return void state more clearly that they have side effects. The ones that return the modified result are supposed to have no side effects, including modifying the original input. Making a method return void implies that it changes its input or some other internal state of the API.

like image 63
luvieere Avatar answered Oct 02 '22 16:10

luvieere


If you had Reverse() return a string, then it wouldn't be obvious to a user of the API whether it returned a new string or the same-one, reversed in-place.

string my_string = "hello"; string your_string = my_string.reverse(); // is my_string reversed or not? 

That is why, for instance, in Python, list.sort() returns None; it distinguishes the in-place sort from sorted(my_list).

like image 34
Mark Rushakoff Avatar answered Oct 02 '22 16:10

Mark Rushakoff