Comment Re:GOTO is a crutch for bad programmers (Score 1) 677
void func()
{
if (AquireResource1())
{
if (AquireResource2())
{
if (AquireResource3())
{
DoStuffWithResources();
Cleanup3();
}
Cleanup2();
}
Cleanup1();
}
return;
}
I know some people are allergic to triangles, but I like them because it makes it easy to see what code is potentially skipped and which code is definitely going to run (barring a crash) at any given time.
Another alternative:
void func ()
{
Resource resources[3];
if (AcquireResource1(&resources[0]) &&
AcquireResource2(&resources[1]) &&
AcquireResource3(&resources[2]))
{
DoStuff(Resources);
}
ReleaseResources(resources);
return;
}
This is essentially simulating RAII semantics in C. You might even pack all three AcquireResource[1|2|3] functions into a common AcquireAllResources and in so doing make the if less ugly.
I don't like that goto code above because I think it makes it difficult to follow every flow of execution. You may be used to it and think the opposite.