3

So I came across an old blog post from Robert Martin (Uncle Bob) that talks about how the Structured Programming discipline convinced programmers to take the GOTO statement out of their code, even when its only implicit like in this example that he gave:

if (a>10)
  b++;
else
  b--;

He said that was bad practice because it basically would correlate to this code if written in FORTRAN:

    IF (A-10) 20,20,30
20  B = B - 1
    GOTO 40
30  B = B + 1
40  ...

To me, that makes sense. An If block essentially acts like a GOTO statement under the hood so why use it. (Of course, in the back of my mind, I'm thinking there's no way to get around some kind of JUMP instruction in the underlying assembly language, which is basically a GOTO statement, but I ignored that thought and moved on.)

But then he said:

you could restrict your program to three different control structures: Sequence, Selection, and Iteration

and that with a Selection structure, you could write code like this:

if (someBooleanValue())
   doThisStep();
else
   doOtherStep();

That's supposed to be a better version of an If block than the GOTO version I mentioned above because it used a Selection structure. However, I don't really see the difference between them. What is the fundamental difference?

The only thing I can figure is that is has something to do with what he said here:

Dijkstra's argument was that a structured program can be easily analyzed because the state of the system at any line of code, depends only on the boolean values being tested by selection and iteration, and the list of calling procedures on the stack.

Somehow it seems like there is a deeper principle here that I'm missing. I get that you can keep track of the state of a program using boolean variables but what does that have to do with these two If blocks?

1 Answers1

7

What is the fundamental difference?

There isn't one.

The reason your friend's argument falls apart is because any if-else can be rewritten using a goto. In fact, at the processor level, every branch instruction (including the conditional ones) is a goto.

Structured programming is the act of using a structured programming language to write structured code, not musing about what the equivalent code in another language is, or what is happening under the language's hood.

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
  • So if there's no difference between those two `If` blocks, then I guess I'm trying to understand the deeper point of the article and the real value of structured programming. I think Uncle Bob was trying to compare/contrast the "awful" FORTRAN example with the "sequence, selection, and iteration" style of programming, but I just don't see the underlying principle he's trying to share. I guess that's a different question than the one I asked, but it's the one I was trying to formulate. – BarrettNashville Jul 27 '17 at 19:38
  • I suppose that the simple answer to that question is "strictly-structural languages don't contain goto instructions as part of their language specification." Corollary: you don't need goto to write programs in such languages; you can write any such program using "sequence, selection and iteration," without ever involving a goto. If you're still wondering why people care about this, look up Dijkstra's famous paper "Goto Considered Harmful." – Robert Harvey Jul 27 '17 at 19:40
  • 3
    Oh, I think you're right. I was reading too much into that part about FORTRAN. I thought he was saying something like "See _this_ Java example is the same as _this_ FORTRAN example, its just GOTO under the hood and that's bad. Here's the better way with structured programming." I guess he was actually saying "_This_ Java example represents the good code we write now. Compare that to _this_ bad FORTRAN code we used to write. Here's how we improved things: structured programming." – BarrettNashville Jul 27 '17 at 19:47
  • Yes, exactly ... – Robert Harvey Jul 27 '17 at 19:56