Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any disadvantage to returning this instead of void?

Say instead of returning void a method you returned a reference to the class even if it didn't make any particular semantic sense. It seems to me like it would give you more options on how the methods are called, allowing you to use it in a fluent-interface-like style and I can't really think of any disadvantages since you don't have to do anything with the return value (even store it).

So suppose you're in a situation where you want to update an object and then return its current value. instead of saying

myObj.Update();
var val = myObj.GetCurrentValue();

you will be able to combine the two lines to say

var val = myObj.Update().GetCurrentValue();

EDIT: I asked the below on a whim, in retrospect, I agree that its likely to be unnecessary and complicating, however my question regarding returning this rather than void stands.

On a related note, what do you guys think of having the language include a new bit of syntactic sugar:

var val = myObj.Update()<.GetCurrentValue();

This operator would have a low order of precedence so myObj.Update() would execute first and then call GetCurrentValue() on myObj instead of the void return of Update.

Essentially I'm imagining an operator that will say "call the method on the right-hand side of the operator on the first valid object on the left". Any thoughts?

like image 322
George Mauer Avatar asked Sep 11 '08 17:09

George Mauer


People also ask

When would you use a void method vs a value returning method?

When to use Void or Value-Returning Functions: Void Function: when must return more than one value or modify any of the caller's arguments. Void Function: when must perform I/O. Void Function: when in doubt, can recode any value-returning function as void function by adding additional outgoing parameter.

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

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

Why would you use a void method?

In most cases, we use void methods when we need to perform an operation with some kind of side effects. The most common use case of void methods is System. out. println which prints desired text to standard output.

What should a void returning method do if it fails to execute?

It exits the function and returns nothing.


2 Answers

The only disadvantage I can see is that it makes the API slightly more confusing. Let's say you have some collection object with a remove() method that would normally return void. Now you want to return a reference to the collection itself. The new signature would look like:

public MyCollection remove(Object someElement)

Just looking at the signature, it's not clear that you're returning a reference to the same instance. Maybe MyCollection is immutable and you're returning a new instance. In some cases, like here, you would need some external documentation to clarify this.

I actually like this idea, and I believe that there was some talk in retrofitting all void methods in Java7 to return a reference to 'this', but it ultimately fell through.

like image 58
Outlaw Programmer Avatar answered Nov 16 '22 01:11

Outlaw Programmer


I know in Java they're actually thinking about making this standard behaviour for void methods. If you do that you don't need the extra syntactic sugar.

The only downside I can think of is performance. But that's easilly measured. I'll get back to you with the results in a few minutes :-)

Edit:

Returning a reference is a bit slower than returning void .. what a surprise. So that's the only downside. A few more ticks when calling your function.

like image 45
Mendelt Avatar answered Nov 16 '22 02:11

Mendelt