3

I have this code that we can write in two ways

First Way

void func(int val)
{
     if(val == 0)
          return;

     // Do something ...
}

Second Way

void func(int val)
{
     if(val != 0)
     {
          // Do something ...
     }          
}

The Question:

Is there any reason to use the first way ? Is there any Advantage to use the first way ( in C++ or in C# )

Yanshof
  • 9,659
  • 21
  • 95
  • 195
  • Please search this site for "return early", this has been discussed hundreds of times. – Mat Aug 02 '12 at 06:12
  • Use the one which you like/ or able to read better ! ! You can also have a good [look here](http://programmers.stackexchange.com/questions/18454/should-i-return-from-a-function-early-or-use-an-if-statement) – V4Vendetta Aug 02 '12 at 06:13
  • The first one of-course avoids the Anti-head arrow pattern: http://lostechies.com/chrismissal/2009/05/27/anti-patterns-and-worst-practices-the-arrowhead-anti-pattern/ – parsh Aug 02 '12 at 06:16

8 Answers8

10

The main reason to use the first way is to reduce nesting in your source file.

serge.karalenka
  • 980
  • 1
  • 15
  • 29
5

See Invert “if” statement to reduce nesting.

Basically this becomes important when '// Do something ...' is a lot of code.

Community
  • 1
  • 1
tymtam
  • 31,798
  • 8
  • 86
  • 126
5

Imagine, that there's more than one condition, and look at this:

if (...)
{
  if (...)
  {
     if (...)
     {
        if (...)
        {
           if (...)
           {
             ...
           }
        }
     }
  }
}

and this:

if (!...)
  return;

if (!...)
  return;

if (!...)
  return;

if (!...)
  return;

if (!...)
 ...

I think, the second one is more readable an convenient.
If there's one or two conditions, the difference is insignificant.

Dennis
  • 37,026
  • 10
  • 82
  • 150
3

Yes there are advantages as mentioned in previous answers. Since it is tagged in C++ care should be taken for freeing the allocated memory in case of using First Way.

For Eg there can be bad code like this

void func(int val)
{
     Foo *obj = new Foo();
     if(val == 0)
          return;

     // Do something ...
    delete obj;        // Possible Memory leak
}
Jeeva
  • 4,585
  • 2
  • 32
  • 56
  • If this was C your comments are correct. In modern C++ code is never written like that (though C programmers often write bad C++ like this). RAW pointers are encapsulated in RAII objects to prevent this exact scenario. That is why early return is not only possible but very common in C++ but rare in C. – Martin York Aug 02 '12 at 11:28
  • @LokiAstari: Totally agree with you – Jeeva Aug 02 '12 at 11:32
  • 1
    @Picarus: **THE** most important concept of C++. A quick google gives me: http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization – Martin York Aug 03 '12 at 21:32
1

There is no difference - whether to use one or the other depends entirely on your subjective preference for one or the other, influenced by readability, understandability, etc

logicnp
  • 5,796
  • 1
  • 28
  • 32
1
  • if you want to exit quick on a defined value, so return before should be better,
  • but if this case is rare, so probably it's better to directly work on good data.

depending on which language you use, implications on performance should be greater, or insignificant.

if it's intended for clarity, so make some functions called after conditions should be good. Some parts manage different cases, some parts process it.

maintainability and readability should be improved this way.

N4553R
  • 188
  • 4
0

if you call as the first way then its not run the rest of the things. because it return directly. but if you use second way the codes inside the will run.

DevT
  • 4,843
  • 16
  • 59
  • 92
0

Void methods do not require a return statement always. But you can use a 'return' in a strategic place to avoid executing unnecessary statements when you have encountered an exit condition.

Sandeep Pathak
  • 10,567
  • 8
  • 45
  • 57