Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

rsegal

Is this bad practice?

This topic is 5423 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it bad practice to return from a function in the middle of a for loop? For example,
int StupidFunc()
{
  for (int i = 0; i < 10; i++)
  {
    if (i == 2)
      return 1;
  }

  return 0;
}
 
I''ve always assumed in this case it''s better to use a while loop and rewrite the function as something like,
int StupidFunc()
{
  bool bFlag = false;

  while ( (i < 10) && (!bFlag) )
  {
    if (i == 2)
      bFlag = true;

    i++;
  }

  if (bFlag)
    return 1;
  else
    return 0;
}
 
I guess it may depend on what is being done within the loop but still I wonder would most of you consider it bad practice to use the for loop form?

Share this post


Link to post
Share on other sites
Advertisement
Well, you normally say that a function should contain only one return statement! But sometimes that is just way too impractical so do a case-by-case ruling



Human beings, by changing the inner attitudes of their minds, can change the outer aspects of their lives.

William James (1842 - 1910)

Share this post


Link to post
Share on other sites
I lost marks in my C++ Programming class for returning out of a function. I argued and bitched and cited examples like the Win32 API Message Pump to no avail.

Stupid god damn course. I wish everyone in that school would die.

Share this post


Link to post
Share on other sites
My opinion is that the first function is more readable than the second. The second function the testing of bFlag looks like duplication of code which the if statement(in the first function) in the for loop is supposed to handle anyway. I can''t see how return in the middle of a loop(in that example anyway) can be considered bad practice.



Share this post


Link to post
Share on other sites
Returning from the middle of a function is much like a "goto". As in, you change the execution point with out being "obvious" about it. The common practice is that a subroutine should have one exit point, the bottom, so that you do not need to understand the entire function to tell where its getting returned from. However in simple functions that are less then a page long, returning from the middle can add to clarity.

Share this post


Link to post
Share on other sites
quote:
Original post by _buu_
However in simple functions that are less then a page long, returning from the middle can add to clarity.

...and functions longer than one page are considered
bad practice, too by some text books.


Share this post


Link to post
Share on other sites
Multiple returns is a worse practice in C than it is in C++. In C++ you can use RAII to guarantee that all acquired resources are released when a function returns, no matter where the return was called. In C, there''s no good (standardized) mechanism to do the same thing. So by obeying SESE you have a better chance of not leaking resources. The same argument can get extended to other languages such as classic Pascal.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!