Home

hersto:  Source Code Indentation / Goto Statements

 

Rant to the author:

    

Check for a response

Please read Source Code Indentation for the basic idea first.

Although considered not a good programming style, goto statements are available in some languages and can be used. If used, they have a strong impact on the controll flow and that's why they should be emphasized accordingly.

goto statements don't follow nesting levels, they just jump to another location in the same function. So why do poeple nest them below if or other statements??
The easiest way to indent them is to place them at the same indentation level as the function body.

An Example:

 1:  function SomeSpagethis()
 2:  {
 3:      tWays WayOfDoingIt= eDoItTheStandardWay;
 4:
 5:  lRedo:
 6:
 7:      int Z= 0;
 8:
 9:      DoStandardPreparations();
10:
11:      if(WayOfDoingIt == eDoItTheNewWay)
12:          PrepareForNewWayParsing();
13:
14:      for(int C= 0; C<NumElements; C++ )
15:      {
16:          FILE* F= fopen(FileName[C], "rb");
17:
18:          char[1234] Buffer;
19:
20:          int Err= 0;
21:
22:          while(ReadALine(Buffer, sizeof(Buffer), &Err))
23:          {
24:              TrimLine(Buffer, sizeof(Buffer), WayOfDoingIt);
25:
26:              tSomeEnum rc= ProcessLine(Buffer, WayOfDoingIt);
27:
28:              if(rc == tFailedBecauseOfSyntaxError)
29:              {
30:                  fclose(F);
31:                  WayOfDoingIt= eDoItTheNewWay;
32:  goto lRedo;
33:              }
34:          }
35:
36:          fclose(f);
37:
38:          if(Err == eCannotReadFile)
39:          {
40:              // let's try it the new way...
41:              WayOfDoingIt= eDoItTheNewWay;
42:
43:  goto lRedo;
44:          }
45:      }
46:
47:      ProcessResultsOfFileReading();
48:  }

This is a piece of code which tries to read in a bunch of files. It is structured with for and while loops, but under certain circumstances the whole process will be started over again with some slight modification of the processing scheme. This is expressed with a goto statement that jumps straight out of the code to a label lRedo just at the moment when failure of reading the file is detected.

Some annotation: This page does not want to promote goto statements for this type of code or for any other coding situation. It just wants to propose how to indent goto statements and the corresponding labels if goto statements are used.

A more elaborate scheme:

A more elaborated scheme than described above could consider the nesting level the goto statments and labels are in: if a label and all the gotos that reach it are contained in the same nesting level, e.g. a while loop, then the label and its goto statements are indented the same level as the loop.

function DoSomething()
{
    int X= 0;

    for(int C= 0; C<100; C++ )
    {
    lDings:

        DoSomeProcessing();

        if(SomeCondition)
    goto lDings;


        while(SomeCondition)
        {
        lFoo:

            DoSomething();

            if(SomeCondition)
        goto lFoo;

            if(SomeCondition)
    goto lDings;

            DoSomeThing();
        }
    }
}

 

__.-.__
end of document