top | item 8375651

(no title)

EddSeabrook | 11 years ago

Line 457-470 of vicissicalc.c: why do you use else if here rather than switch-case?

discuss

order

ufo|11 years ago

Might have been because of the `else if (ch == 'q') break;` line. If he used a switch statement he would have needed to use a goto to break out of the loop.

abecedarius|11 years ago

That's a reasonable guess, but a return would work there. I think I did it this way because all the breaks you need in a switch are noisy -- too noisy if you'd like to write one action per line. However, you can mute the noise by lining it up:

        switch (getchar ()) {
            break; case ' ': enter_text ();
            break; case 'f': view = formulas;
            break; case 'h': col = (col == 0 ? 0 : col-1);
which also makes oops-I-forgot-the-break hard to miss. I hadn't thought of that pattern yet. (You could define a macro for "break; case" too; my friend Kragen calls that IF.)

But I mostly stopped coding in C after around this time.