Skip to contentSkip to conversation

Patch?: linux-2.5.59/sound/soundcore.c referenced non-existant errno variable

DISCUSSION Adam J. Richter 3 participants 4 messages Jan 19, 2003 – Jan 21, 2003

lore
Share & tools
lore
#0 lore raw
Quoted 3 lines

In original message I asked whether I should convert firmware loader to the "if (error) goto quit;" style to move error handling to one place, but only answer I got was 'Ask Rob' ;-)

To my knowledge, a goto in this case is not necessary for
avoiding code duplication. If there are a small number of failable steps that may need to be unwound, you could adopt the style of my patch (which shortened the code slightly):

       if (step1() == ok) {
		if (step2() == ok) {
			if (strep3() == ok)
				return OK;
			undo_step2();
		}
		undo_step1();
	}
	return failure;

	If the nesting gets any deeper than this, then a more

understandable solution for readability than using goto would be to define a separate inline routine.

In general, I recommend using goto only when it is
topologically necessary to avoid code duplication or due to some compiler quirk where you want to sqeeze a few more cycles out of code in a critical path. That way, the use of goto basically flags these unusual cases for other programmers.

Examples of cases where a goto really is needed to avoid code
duplication include a switch() or a group of if() branches need to flow back together after starting out by doing different things, and where you want to break from an outer loop and for some reason don't want to write the outer loop as an inline routine (for example, you also have a "return" statement for the existing subroutine in this loop).

Adam J. Richter     __     ______________   575 Oroville Road
[email protected]     \ /                  Milpitas, California 95035
+1 408 309-6081         | g g d r a s i l   United States of America

"Free Software For The Rest Of Us."

#1 lore raw
Quoted 5 lines · Adam J. Richter

To my knowledge, a goto in this case is not necessary for
avoiding code duplication. If there are a small number of failable steps that may need to be unwound, you could adopt the style of my patch (which shortened the code slightly):

Quoted 9 lines
       if (step1() == ok) {
		if (step2() == ok) {
			if (strep3() == ok)
				return OK;
			undo_step2();
		}
		undo_step1();
	}
	return failure;

The "undo_stepX()"'s pollute the CPU's cache, and (even much worse) the gentle reader's.

Quoted 3 lines

If the nesting gets any deeper than this, then a more
understandable solution for readability than using goto would be to define a separate inline routine.

Can't be done (cleanly) in many cases due to function semantics in C, polutes CPU cache as above, screws up or gives bad code due to compiler bugs. Plus has the gentle reader who wants to check error handling chasing all over the place.

Quoted 5 lines

In general, I recommend using goto only when it is
topologically necessary to avoid code duplication or due to some compiler quirk where you want to sqeeze a few more cycles out of code in a critical path. That way, the use of goto basically flags these unusual cases for other programmers.

IMVHO, any general criterion that is not strictly based on code understandability, possibly mitigated by a justified need of maximal speed, is flawed. This might come close, but won't cut it for me.

Signature · 4 lines
Dr. Horst H. von Brand                   User #22616 counter.li.org
Departamento de Informatica                     Fono: +56 32 654431
Universidad Tecnica Federico Santa Maria              +56 32 654239
Casilla 110-V, Valparaiso, Chile                Fax:  +56 32 797513
#2 lore raw

On Mon, 20 Jan 2003, Horst von Brand wrote:

[ the war between effeciency and maintainability continues ]

Quoted 16 lines
Quoted 5 lines · Adam J. Richter

To my knowledge, a goto in this case is not necessary for
avoiding code duplication. If there are a small number of failable steps that may need to be unwound, you could adopt the style of my patch (which shortened the code slightly):

Quoted 9 lines
       if (step1() == ok) {
		if (step2() == ok) {
			if (strep3() == ok)
				return OK;
			undo_step2();
		}
		undo_step1();
	}
	return failure;
Quoted 2 lines

The "undo_stepX()"'s pollute the CPU's cache, and (even much worse) the gentle reader's.

Given the probably effect of the steps on the cache, I think the readability argument is a better one, particularly if you have more than a few steps. There is an effect, but it's relatively small. But use of goto need not be unreadable.

if (step1() != ok) goto errex0;
if (step2() != ok) goto errex1;

  if (step3() == ok) {
    if (step4() == ok) return OK;
    undo_step3();

}
undo_step2();
errex1:
undo_step1();
errex0:
/* in case there is something other than just return, jump here */ return FAIL;

Less indenting, and the undo's falling through look visually like a switch without the overhead.

I have not looked at the code this generates, it's a comment on human readability rather than an actual implementation, and I'm sure someone will argue that the first failure should just be a return if there's nothing else which needs to be done. On the other hand the return inline would be more bytes, so someone else can argue against.

Signature · 3 lines
bill davidsen <[email protected]>
  CTO, TMR Associates, Inc
Doing interesting things with little computers since 1979.
#3 lore raw

Bill Davidsen <[email protected]> said:

[...]

Quoted 5 lines

I have not looked at the code this generates, it's a comment on human readability rather than an actual implementation, and I'm sure someone will argue that the first failure should just be a return if there's nothing else which needs to be done. On the other hand the return inline would be more bytes, so someone else can argue against.

Older gccs used to generate horrible code for several "return foo;" in the function, dunno how it is today.

Besides, today there might be nothing else to do, but that can change. In that case you'd better have all exits together.

Signature · 4 lines
Dr. Horst H. von Brand                   User #22616 counter.li.org
Departamento de Informatica                     Fono: +56 32 654431
Universidad Tecnica Federico Santa Maria              +56 32 654239
Casilla 110-V, Valparaiso, Chile                Fax:  +56 32 797513