Getting back to some C and C++ bashing, a discussion was brought up by some language lawyers about always using structured code constructs instead of non-structured state machines. This brings up a problem when translating non-structured or semi-structured languages like BASIC.
In the following example, scope rules prevent C from creating equivalent syntax:
PROC Z(X,Y)
IF X=Y
LET N=1
LET Y=0
ELSE
LET N=2
LET X=X+1
ENDIF
PRINT N,X,Y
ENDPROC
The equivalent C code would normally be:
void z(int x,int y) {
if (x==y) {
int n;
n=1;
y=0;
} else {
int n;
n=2;
++x;
}
printf("%d/t%d/t%d/n",n,x,y);
}
This example is a little contrived but in C, the variable n goes out of scope in both the if
and the else
so it won’t even compile. When translating from BASIC to C, those curly brace scoping rules become a major pain! There are 2 ways to solve it but both are ugly.
One is to define preprocessor macro magic in a header file written concurrently to the C file so any variable declaration is moved to just inside the curly brace after the function definition.
The other is to reinvent the structured programming commands themselves using a non-structured state machine. I’ve explained this position in my YAB2C++ thread. This didn’t appease the language lawyers.
I had to get this off my chest or I wouldn’t sleep tonight. I just had reread the linked thread. Good-night all!