:: Re: [DNG] Studying C as told. (For …
Αρχική Σελίδα
Delete this message
Reply to this message
Συντάκτης: Rainer Weikusat
Ημερομηνία:  
Προς: dng
Αντικείμενο: Re: [DNG] Studying C as told. (For help)
Hendrik Boom <hendrik@???> writes:
> On Sat, Jun 25, 2016 at 01:21:33AM +0200, Irrwahn wrote:
>> On Fri, 24 Jun 2016 14:33:21 -0400, Steve Litt wrote:
>> [...]
>> > I often use continue and break, but every time I do, I make a mental
>> > note that I'm decreasing modularity and thus reducing the scalability
>> > of my code. Of course, I might have also increased my code's
>> > readability by reducing redundant indentation. It's a tradeoff.
>>
>> Well put. Only to add that a similar argument can be formed
>> regarding the more general[1] "goto" statement.[2] Structured
>> programming is a virtue, but one has to hang the tenets high
>> enough to comfortably walk beneath.[3]
>>
>> IMHO the people that (ab)used Dijkstra's famous essay (with
>> the original title "A Case Against the Goto Statement") as
>> the foundation of some kind of religion did him and the
>> programming community as a whole a bad service. For the
>> interested, David Tribble's "Go To Statement Considered
>> Harmful: A Retrospective"[4] makes for a good read.
>
> If you actually read Dijkstra's original letter, you'll find he is
> concerned with devising some kind of coordinates for measuring progress
> through execution, so that proofs of correctness can be hung on them, so
> to speak. break and continue are both forms of early exit from a
> statement, and do not interfere with his reasoning.


That's because they do not represent 'multiple points of exit' in the
sense someone familiar with machine code programming at about the time
Dijkstra wrote this text had understood it.

Below is an example of a multi-exit subroutine:

-----------
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>

static jmp_buf was_odd;

static void is_odd(unsigned x)
{
    if (x & 1) longjmp(was_odd, 1);
}


int main(int argc, char **argv)
{
    unsigned x;


    ++argv;


    while (*argv) {
    x = atoi(*argv++);


    if (setjmp(was_odd)) {
        printf("%u is odd\n", x);
    } else {
        is_odd(x);
        printf("%u is even\n", x);
    }
    }


    return 0;
}
-----------


It's 'multi-exit' because after calling it, control-flow of the main
program can resume in two different places. OTOH (uncompiled/ -tested)

static int is_odd(unsigned x)
{
    if (x & 1) return 1;
        return 0;
}


isn't.