[Gluster-devel] An interesting take on GoTo statement by Dijkstra -

Kaleb KEITHLEY kkeithle at redhat.com
Thu Jan 15 18:12:56 UTC 2015


On 01/15/2015 08:02 AM, Nagaprasad Sathyanarayana wrote:
>> On 15-Jan-2015, at 5:38 pm, Vijay Bellur <vbellur at redhat.com> wrote:
>>
>>> On 01/15/2015 12:13 PM, Nagaprasad Sathyanarayana wrote:
>>> In a quest to find why good programmers are wary of the "Go To"
>>> statement, came across this interesting article by /Edsger W. Dijkstra/ .
>>>
>>> http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html.
>>
>> goto statements that cause one to go back & forth across the code are annoying to me. However, if goto statements are used to return control from a common point within a function, they enhance readability and also help in ensuring that we perform necessary cleanup/terminal actions for a function in a single place.
>>
>> It is to be noted that Dijkstra's article appeared before C was invented. If you are interested in understanding more about goto, Knuth's paper titled Structured Programming with go to statements [1] and a lkml thread on the same [2] can be helpful.
>>

FWIW, if you consider the trivial example:

#include <stdio.h>

void
Goto(int ctl)
{
     if (ctl == 0) {
         printf ("foo\n");
         goto out;
     }

     if (ctl == 1) {
         printf ("bar\n");
         goto out;
     }

     if (ctl == 2) {
         printf ("baz\n");
         goto out;
     }

     printf ("nothing happened\n");

out:
     printf ("returning\n");

}

void
StructuredGoto(int ctl)
{
     do {
         if (ctl == 0) {
             printf ("foo\n");
             break;
         }

         if (ctl == 1) {
             printf ("bar\n");
             break;
         }

         if (ctl == 2) {
             printf ("baz\n");
             break;
         }

         printf ("nothing happened\n");
     } while (0);
     printf ("returning\n");
}

If you compile to assembly, both functions generate exactly the same 
code, both with and without optimization. (gcc-4.9.2, clang-3.4)

Gluster prefers the use of goto as it eliminates a level of indentation. 
We have a somewhat artificial limit of 80 columns and functions that 
have too many levels of indention will get kicked out in review.

Since the goto idiom that Gluster uses generates the same code — which 
is what matters — I gave up my short-lived battle for not using it.

--

Kaleb



More information about the Gluster-devel mailing list