Hacker News

"possibly the nastiest piece of abuse of the C switch statement rules I know of"

42 points by pavel_lishin ago | 11 comments

pavel_lishin |next [-]

I did a double-take when I saw the name of the author, as I've been playing his puzzle packs for years. (Not to mention Putty - which I haven't used in probably a decade, but was my go-to in Windows.)

svilen_dobrev |next |previous [-]

also check:

How to Get Fired Using Switch Statements & Statement Expressions\

https://blog.robertelder.org/switch-statements-statement-exp...

janderland |next |previous [-]

Haha, love this. It’s funny they’re saying “don’t use GOTO” because this approach makes it obvious that switch cases are equivalent to GOTO which can transport you into one of the impossible of statements.

1718627440 |root |parent [-]

Yes, goto and switch cases are equivalent in C, but the "goto" from "GOTO considered harmful"/"A case against the GO TO statement" is called "longjmp" in C.

kmoser |root |parent [-]

> Yes, goto and switch cases are equivalent in C

Is that really true though? Because switch/case only lets you jump forward, whereas goto lets you jump in either direction.

adrian_b |root |parent [-]

Correct. They are not equivalent.

During the early seventies, it was argued (inclusively by Knuth) that all undesirable effects of using "goto" can be prevented if "goto" is restricted to jump only forwards and only to the same block or to outer blocks, i.e. never backwards or into inner blocks.

This kind of restricted "goto" was available for example in the language Xerox Mesa, a very innovative language, which has influenced many later languages, including Modula, Ada and Python.

In the comment thread from TFA, someone wrongly says that completely avoiding "goto" in programs is fine, because the programs without "goto" "would not loose any amount of expressive power".

This is false. Unfortunately many more people are aware of the research paper that claimed that using "if ... then .. else ..." and "while ... do ..." is sufficient for writing any problem, than of the fact that later research papers have demonstrated that this claim is true only when you accept a more inefficient program. There are instances of using "goto" which can be replaced with the structures mentioned above, or with any other of the program structures that were favored by the "structured programming" fashion, only by adding superfluous variables or superfluous jumps, which increase program size and execution time.

Nonetheless, the restricted form of "goto" mentioned above, together with the classic alternative and iterative program structures, is sufficient to implement any program with optimum efficiency.

1718627440 |root |parent [-]

> Correct. They are not equivalent.

Sorry I was imprecise. Yes they are not completely equivalent. I meant on a conceptual level in that they are a jump, i.e. a switch is a special kind of goto. A case never lives outside of the switch, but actually in a similar fashion, a goto never lives outside of the function.

> This is false. Unfortunately many more people are aware of the research paper that claimed that using "if ... then .. else ..." and "while ... do ..." is sufficient for writing any problem, than of the fact that later research papers have demonstrated that this claim is true only when you accept a more inefficient program.

I honestly had the problem surprisingly often that I do want a loop, but the entry position and the position of the conditional are not the same in the loop. There is just no syntax for that, and I find that quite dumb, that this is where the evolution of basic structure stopped. When I used Python, I often duplicated the initial part or used the fact that Python has an else branch for a loop. But in a static language like C, this means that you can't declare the variables only in the loop, which bothers me, or that you also need to duplicate all the error handling, which really becomes a mess and liability. So now in C, I either use a goto to get into the loop or write an infinite loop and place the loop condition manually in the middle and mark the fact that it is the loop condition with a comment. But it feels lacking in expressing that I need to use a comment for that.

TZubiri |previous [-]

> exposed a compiler bug

i’m 99% sure this was unnecessary and hubrish

whats that phrase about if you write the most clever code you can within the writing context, future you without that context won’t be able to understand it.

well you were so clever that the compiler didnt understand it

pavel_lishin |root |parent [-]

Right, the author doesn't actually come out and say it, but it's very clear that the code is far too clever and completely un-intuitive to anyone who didn't write it ten minutes ago.

adrian_b |root |parent [-]

This does not change the fact that it was a compiler bug, because the offending program is still correct according to the language specification, even if the possibility of such code had not been anticipated by the compiler author, who had used heuristics to identify unreachable code, instead of a complete parsing.

TZubiri |root |parent [-]

Here lies the body of William Jay,

Who died maintaining his right of way.

He was right, dead right, as he sped along,

But he’s just as dead as if he’d been wrong