Archived

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

Is this bad practice?

This topic is 5112 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
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
quote:
Original post by GroZZleR
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.

Citing Win32 for clarity won''t win you any arguments.

Share this post


Link to post
Share on other sites
In my first week of being at Uni i had an arguement with a lecturer over the same thing, i cant remember what the function was but it was trival and it made sense to return at the two points i was doing so, yet she insisted on one exit point and threatened to mark me down if i had more than one, so one rewrite later it had one exit point and was written badly... and people wonder why some companies wont take ppl directly from Uni...

Share this post


Link to post
Share on other sites
Personally, I prefer using for-loop because the controlling conditions are clearly seen unless while-loop
Chances are, the tendency to loop forever is lesser using the for-loop.

As for return out of the function, what u could do is, declare a returning variable in the function, or in ur case, a boolean, since u return either a 1 or 0.

after you''ve found what you want in the for-loop, set the returning variable to true and break from the loop coz it''s not efficent to keep running after you''ve found what you want.

In the end, u''ll only return ONE value, with is the returning variable, instead of two or more for this case.

Share this post


Link to post
Share on other sites
"Good practice" could mean readability and simplicity...


int StupidFunc()
{
for(int i=0; i < 10;i++)
if (i == 2)
return 1;


return 0;
}


Your first function looks more good style than the second

[edited by - owl on December 14, 2003 11:00:43 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by _the_phantom_
In my first week of being at Uni i had an arguement with a lecturer over the same thing, i cant remember what the function was but it was trival and it made sense to return at the two points i was doing so, yet she insisted on one exit point and threatened to mark me down if i had more than one, so one rewrite later it had one exit point and was written badly... and people wonder why some companies wont take ppl directly from Uni...

Most university teachers were brought up on the "single entry, single exit-point" mindset.

I have never actually been told it to my face or had to deal with it, only really read about it. It really depends on who you get. Like Si said, I bet it was a bigger deal in C because you typically would have to clean up after yourself (malloc/open handles) at the end of the function. So nice not to have to deal with archaic languages.

Share this post


Link to post
Share on other sites
Havinbg to clean up after yourself in C more than C++, yes to some extent. but citing Malloc is a bit misdirecting since after all, you have to free what you new in C++ too

Tell your dusty old professor to go shove it, if it works use it. Not saying you should revert to only using GOTO though, the same rule goes for common sense, use it. If you don''t have it... uhm, buy some :D

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Write code as if anyone drunk off their ass at 3AM could comprehend it.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The second function wasn''t bad, but why include:

if (bFlag)
return 1;
else
return 0;


Can you not simply return bFlag?

Share this post


Link to post
Share on other sites
I would include the else just in case the programmer wanted to change some logic but keep the if intact. Less chance for miscoding.

Just because the code doesn''t advance beyond ''return'' doesn''t mean you shouldn''t follow standard structuring practices.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by rsegal
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?



I think it is bad practice to write the second function the way you did because it doesn''t even compile =p

Share this post


Link to post
Share on other sites
No, I don't think that's bad practice at all. I think that's optimizing code. For debugging purposes, that's a lot easier than keeping track of the extra boolean variable and such. Doing good, keep on truckin'.

[edited by - Adam Jo30 on December 15, 2003 1:36:20 AM]

[edited by - Adam jo30 on December 15, 2003 1:37:29 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by rsegal

int StupidFunc()
{
bool bFlag = false;

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

i++;
}

if (bFlag)
return 1;
else
return 0;
}



That should be rewritten:

int StupidFunc()
{
int iFlag = 0;

for( int i = 0; i < 10; i ++ )
{
if (i == 2)
{
iFlag = 1;
break;
}
}

return iFlag;
}

Readable AND just one return point. Supposedly this is good practice because the compiler can do a better job optimizing, or something.

~CGameProgrammer( );

-- Post screenshots of your projects. 100+ posts already in the archives.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
That function is so simple, i dont think returning in the middle of a loop would be unreadable by anyone. The rule of thumb of having only 1 exit point, was made to simplify code. Following it to the exculsion of the oringial intention doesnt make sense. Keeping code simple and functional is the goal, not following a rule without regards to the context.

Good Luck

-ddn

Share this post


Link to post
Share on other sites