tags:

views:

307

answers:

6

Is there any harm for using a GOTO statement to jump down only ? Or are we totally safe ?

What I mean by that is think this as my code,

Some code ...
...

GOTO whereToJump

Some code ...
...

whereToJump: 

Some code ...

When whereToJump point is always below the GOTO statement, is there any security issues?

A: 

goto does not make the code less safe independent of which way you jump.

You structure your code any way you like. It is your code that decides if the code is good/bad. goto maybe appropriate in some cases (no I can's show an example :-) )

The problem with goto is that it may hide the flow of execution in the program and make the code confusing. But if you create easy to read/maintain code then that is not a problem.

Arve
'safe' Is not the correct word. I would say less readable / less understandable / harder to prove correct.
Steven
+1  A: 

This is subject of one of the most famous computer science papers. Go To Statement Considered Harmful by Dijkstra. It essentially states that noone ever needs goto (I know there are a few exceptions as always).

It is 1968 but still very readable today.

Thirler
It spawned the biggest computer science slapfight ever though, so who knows what the end conclusion is...
Jimmy
My goto's have fancy names - if, while, do, switch.
peterchen
This paper was written in a very different context. In those days, `goto` statements were pretty much unrestricted, as in assembly code. That is, people were using `goto` to jump all over the place within programs. It would be equivalent to jumping via `goto` from one function to another. C#'s `goto` is very limited by comparison. That said, you're correct that there's still little need for it. I don't think I've ever hit a case in C# where `goto` was the best option.
Sean Devlin
+1  A: 

It makes sense that only downward jumps makes for much less potential for spaghetti code. ( in my experience, 75% of the headache in debugging legacy GOTO-based code comes from that case when upward gotos result in chaotic looping )

However, given that you are only using downward jumps, it should be very easy to convert to non-goto based code. I'm not sure how much of an improve your gotos would provide.

Jimmy
forward goto's are still a temptingly simple device to break out of nested blocks.
peterchen
+1  A: 

In any more complicated example, surely if you can decide to use the GOTO only to jump forward in the same scope, you could use an if statement. (If it's not in the same scope, it's not really "just jumping forward", is it?)

(And, of course, if your real code is not more complicated than that, you could just get rid of that second block of "Some code...")

detly
A: 

Well, there's really no problem. That code is equivalent to:

Some code ...
...

if (false)
{
Some code ...
...
}

Some code ...

That said, I wouldn't do it. Good use cases for goto are really rare, and other flow control constructs are more idiomatic in C#.

Sean Devlin
A: 

The cardinal reason why GOTOs should be avoided these days is that programmers in general are not used to GOTOs any more (which is a good thing). So if you write code that makes extensive use of GOTOs, chances are your fellow programmers will have difficulty understanding and extending it.

Worse, a GOTO here and there may lead to Broken Window Syndrome as colleagues begin to use more and more GOTOs until you're left with a big bowl of spaghetti.

xkcd puts it best...

alt text

Thorsten79