Multiple return statements Vs goto's

D

Dave Thompson

On Wed, 18 May 2005 19:56:35 +0100, Chris Croughton

(The original criterion I learned was "fit on a lineprinter page" -- 66
lines. By coincidence, that's very close to the normal terminal window
size I use...)
(The former) only if you lined it up yourself very very carefully, for
the overwhelmingly common case of 11" * 6lpi. With usual alignment you
could safely use 10" = 60 lines with .5" margins top and bottom, but
many printout/listing utilities not to mention compiler listings would
take one or two or even more of those lines for headers and labels and
markings. So <50-55 was IME the rule.
I tend to return early for parameter checks, it saves a lot of nested
conditionals and is usually clearer than the "fall through to a common
exit". <snip>

Concur. And for me, also special cases sometimes but pretty rarely.

- David.Thompson1 at worldnet.att.net
 
C

Chris Croughton

On Wed, 18 May 2005 19:56:35 +0100, Chris Croughton


(The former) only if you lined it up yourself very very carefully, for
the overwhelmingly common case of 11" * 6lpi. With usual alignment you
could safely use 10" = 60 lines with .5" margins top and bottom, but
many printout/listing utilities not to mention compiler listings would
take one or two or even more of those lines for headers and labels and
markings. So <50-55 was IME the rule.

True, I should have said "not more than 66 lines" (some compilers
insisted on a special "page throw" comment which took up an extra line
as well). The terminal window in which I am typing this is 64 lines
(and vi takes the bottom line for status and commands) which is very
similar (except that I don't have to punch the cards myself and wait a
week or more for the lineprinter output said:
Concur. And for me, also special cases sometimes but pretty rarely.

Pragmatism. Not to be confused with #pragmatism <g>...)

Chris C
 
M

Mark

In some functions where i need to return multiple error codes at
multiple places, I use multiple return statements. Say for ex.
if (Found == 1)
{
if (val == -1)
return error1;
}
else
{
if (val2 == -1)
return error2;
}
return success;
This is just a simple example. My case have lot of conditional
statements.

the alternate approach is to use goto statements. Let me know the
better way of doing this and which is preferrable (multiple return
statements or Goto's)

I don't like either approaches...
My preference would be to see:

....
int ret = success;
if(Found == 1)
ret = error1;
else if(val2 == -1)
ret = error2;
return ret;
....

Prevents the need of goto statements OR multiple returns...
both of which I try to avoid whenever possible!

Mark
 
W

Walter Roberson

Mark said:
My preference would be to see:
...
int ret = success;
if(Found == 1)
ret = error1;
else if(val2 == -1)
ret = error2;
return ret;
...

Your logic fails to success -- that is, success is assumed until
it is disproved. In most cases, it is better assume failure and only
assert success when one is sure that the conditions are acceptable.
 
M

Mark

Walter Roberson said:
Your logic fails to success --
Take another look at the OP's code (the part you snipped)
I fail to success because in his example, that is what he wanted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,629
Members
45,332
Latest member
LeesaButts

Latest Threads

Top