views:

558

answers:

9

What is the difference between saying:

if (abc == "a")
{
// do something here...
return;
}

and the same as above, but without the return keyword?

I am a C# coder and I know that the return keyword followed by a type or variable returns that item, but in the above context, return seems to be just to exit the code block but does it make any functional or performance change on the code?

Thanks

+13  A: 

"return" exits from the function, not just the enclosing code block. So if your code block was in the context of a function, like so (I don't know C# so I'm using Java syntax):

int thisIsAFunction(int a) {
    if (abc == "a")
    {
        // do something here...
        return 1;
    }
    // do something else here...
}

if abc == "a" then the "do something else here" will not run. But if you removed the return statement inside the if block, then it would run.

David Zaslavsky
+3  A: 

return statement exits the function immediately, so it might have performance benefits as the following code in the function would not be executed at all.

Mehrdad Afshari
I don't believe that this is good advice - it is almost always preferable to have one exit point from a function (i.e. the end) and to structure your logic to accommodate this. Anyone who has to maintain your code will thank you for this.
Richard Ev
Not necessarily. I believe in many cases (e.g. most recursive base cases), the `return` statement makes the code more readable. It depends on the specific case. It might harm or improve readability. But I agree it might act like goto if misused.
Mehrdad Afshari
There are many, many circumstances in which it's not worth contorting a function's logic to satisfy the single-exit-point fetish - not just for performance but for plain readability.
Robert Rossney
One exit point is SUCH a bad argument. IMO it makes your code so much uglier, harder to manage and forces additional processing.
cgreeno
@Robert: +1 single-exit-point-fetish. That made me laugh out loud. Seriously tho, I never understood why people are adamant about having only ONE return statement. For example,in a "keyDown" event if you want to handle only the letter "A", it makes the most sense to just write if(key != 'A') return;
BFree
+1  A: 

In your case, no - but if you had other code after your 'if' statement that you only wanted to run if your statement was false (e.g. if abc != "a"), then the return allows you to bypass that and exit the function / method.

Andy Mikula
A: 

Executing the return statement will make the execution jump out of the method. Without the return, it would simply go on with the next statement instead.

norheim.se
+3  A: 

MSDN

The return statement terminates execution of the method in which it appears and returns control to the calling method. It can also return an optional value. If the method is a void type, the return statement can be omitted.

Example

//this would do nothing
public void method()
{
   return;
}


 //this would return true
 //notice the return type of bool this means
 //the method expects a true\false value
public bool method2()
{
   return true;
}

public void test()
{
    if(method2())
        method()
}

Now if you ran test method2 would always return true and method1 would just end its processing

cgreeno
A: 

Yes, your method does not have return type in this case. e.g.

public void Foo
{
 if (abc == "a")
 {
  // do something here...
  return;
 }
 // some other code
}

This is to say if abd = "a", then exit the method so that some other code won't be executed.

codemeit
+1  A: 

In a loop or case statement, you can use break to achieve this result. This doesn't work on if statements or code blocks in general though.

And yes, return exits the enclosing function.

Rob Lachlan
+2  A: 

The return statement does exit the current method, not just the code block (for/while/if/etc). So it is useful for situations like the following:

public void MyMethod(object myObject)
{
    if (myObject == null)
    {
        return;  // exits method.
    }

    // do something with myObject
}

Additional info: I will point out, that many people prefer to have one exit point in a method, however, it can be useful to do something similar to the example here in some cases. I would always try to find ways to limit the number of return or exit points in your method.

Lloyd Cotten
A: 

It can be a cleaner way of writing code. I typically do it in a guard clause at or near the beginning of a method. If you have an error condition, just "return" out of the method. It saves wrapping the rest of your work in an else block. Seems trivial, but it helps to reduce code complexity.

Tom
The compiler will actually rewrite a method to look like you described. Alternately, you can throw an exception when have an error condition.
StingyJack
Sure, but I use that "return" approach in non-compiled languages as well. And it's not always to handle an exception, it can just be because there's nothing left that needs to be done in the given case.
Tom