Tuesday, April 3, 2007

Discussion about "goto"

There is a discussion about "goto" operation in a program language. In a program, you can jump to the specified label by using "goto".

for (i = 0; i < max; i++)
if (*data[i] == STOP_CODE)
  goto process2;

....
....
....

process2:
....
....

These code is said no good. The following code is said more better.

char flag = 0;
for (i = 0; i < max; i++)
if (*data[i] == STOP_CODE) {
  flag = 1;
  break;
}

if (flag == 0) {
  ....
  ....
  ....
}

process2:
....
....

Or, divide routine to sub routines.

In the discussion, there is an opinion that "goto" makes code difficult to read. I understand it now. But, when I heard the opinion for the first time, I didn't understand it and didn't agree it. Those days, I and my colleagues could not use C language enough, because the game console doesn't have enough spec. We programed most parts in Assembly language. C language seemed able to be used for smart program. But, we had to reduce steps for the real-time processing. So we modified compiled programs in Assembly language and used "goto" operation in C language. If we had to prepare local variables for flags not to use "goto", it took some extra steps.

/* Allocate local variable by controling stack & address register for it. */
char flag = 0;

/* Compare value by using index addressing. After that fork process. */
if (flag == 0) {
  ....
  ....
  ....
}

In those days, we had calculated amount of clocks to do process within the decided time frame. That was a battle that we reduce 20-30 steps in each sub routine. So I did not believe that there is a discussion about the existence of "goto". Programers in high-end machine had seen the next world. I didn't understand them and envied them. For example; if you should not use "goto" in shader language, what do you do?

After 8 years, I'm programing in shader language like Assembly language instead of Assembly language. The world has been evolved?

No comments: