coroutines from hell
Feb. 7th, 2002 12:16 amYou know PuTTY, the SSH client? I ran across a page by its author, Simon Tatham. It's about coroutines. Now, coroutines are cool, they're sort of like a call/return construct across threads -- read the page, it explains nicely. But his implementation is a flaming hack. It combines preprocessor trickery with Duff's device of all godforsaken constructs, so you've got macros generating various brace-unbalanced fragments of code like this stuff:
This coroutine code is in PuTTY. Do you feel soiled yet?
Well. He's got this great quote, anyway:
static int i, state = 0;
switch (state) {
case 0: /* start of function */
for (i = 0; i < 10; i++) {
state = 1; /* so we will come back to "case 1" */
return i;
case 1: /* resume control straight after the return */
}
}
This coroutine code is in PuTTY. Do you feel soiled yet?
Well. He's got this great quote, anyway:
Any coding standard which insists on syntactic clarity at the expense of algorithmic clarity should be rewritten. If your employer fires you for using this trick, tell them that repeatedly as the security staff drag you out of the building.