Does Dijkstra's "Case against the GOTO statement" apply just as much to modern day calling of named encapsulations of code as functions?

StackOverflow https://stackoverflow.com/questions/20353488

Question

I don't know how it worked back in the day, so I've no idea what he was referring to.

But, take JS:

var x = 5;
var foo = function(y) { console.log(2); };
var y = 6;
foo(); // is this not in essence a goto statement?

If not, then what was different about GOTO statements?

Would Dijkstra have opposed JS on these grounds?

Was it helpful?

Solution

In your example, you are calling a subroutine rather than unconditionally and permanently transferring the flow of control (which is what goto does).

If you put code after foo();, that code would get executed after foo() is called.

OTHER TIPS

No, this is in essence a "GOSUB" statement. Did you read the original paper? IMHO Dijkstra was offended by "smearing" state all over your code.

This is a jump statement, not a GOTO statement. Therefore, it does NOT create spaghetti code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top