Comment

Comment by jstimpfle

A bit of a tangent, there are few instances of "do-while" that I've ever ever written and not removed soon after. In practice, I've found that the looping situations that don't easily match the "for (int i = 0; i < n; i++)" pattern are normally "random" enough that it's best to just write "for (;;)" and put explicit checks and breaks inside the body, wherever they naturally fit. Forcing "for (...)" or "while (...)" or "do-while(...)" syntactic constructs is likely to lead to an unnatural sequence of statements. Doing break anywhere is just fine!

    do
    {
        splice(from stdin...);

        if (A)
            handle_a();
            goto out;

        splice(to stdout...);

        if (B)
            handle_b();
            goto out;

    } while (nwritten > 0);

   // do we need some kind of handle_c()??

    out:
        ...
Why make a special case for the "nwritten > 0" condition here? And what's wrong with "break" vs "goto"?

    for (;;)
    {
        thing_a();

        if (A)
            handle_a();
            break;

        thing_b();

        if (B)
            handle_b();
            break;

        if (C)
            handle_c();
            break;
    }
is cleaner in my eyes.
parent jstimpfle 4 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton