Jump to content
  • Advertisement
Sign in to follow this  
Dragonion

To goto or not to goto?

This topic is 2517 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.


Recommended Posts

Advertisement
I find them useful to do cleanup in functions that can fail, so I dont have to free all the resources everywhere, I can just goto the end, and put all the frees there

Share this post


Link to post
Share on other sites
I never use goto statements. It's generally unsafe to change the flow of an application in this way. Almost in every situation there are more suitable techniques that are safer, and which won't create spaghetti.

Share this post


Link to post
Share on other sites
I think you need to define the context of this question; in some languages you would be highly restricted if there weren't any "goto"s. Making some assumptions, I think Dijkstra's opinion on this matter might be an interesting read ("Edgar Dijkstra: Go To Statement Considered Harmful" http://wwwens.uqac.c...jkstra_Goto.pdf)

I'm curious about the intent behind the poll in this thread; are you meaning to form an opinion based on the general consensus?

Share this post


Link to post
Share on other sites
I've used one goto in the say... 18 years I've been programming. It was a throwaway test app that I had to trigger cleanup code before exiting the function and had done it so slapdash that a finally block wouldn't deal with it; and it amused me to write such horrible code.

If you used a goto in production code I would fire you on the spot.

Share this post


Link to post
Share on other sites
I use [font="Courier New"]goto[/font]. Quite a bit.

I use it exclusively in object-oriented C programming, because C lacks useful constructs such as RAII and exceptions. It is better to have a forward-jumping goto into a sequence of rollback operations as you initialize members on your constructor code than any other way of handling failures. The use of [font="Courier New"]goto[/font] gives clarity and structure where no other construct can.

For those of you who are too young to understand what Dijkstra wrote, his poins was that alternatives to [font="Courier New"]goto[/font] should be emphasized and chosen in many situations. At the time, such as when I was a student, much code was written in non-structured langauges like FORTRAN IV or BASIC, and the use of [font="Courier New"]goto[/font] was required by the language itself. Most programs were spaghetti code. Dijkstra was involved in designing a new language, one that could be used to teach and to reason about computing -- he was not advocating a dogmatic methodology for future generations.

When a dogmatic anti-goto zealot tells you never to use that control construct, stop and ask for a clarification on the reasons why. If he or she can not tell you any reason other than the ipso dixit of "Dr. Dijkstra wrote a paper saying it was harmful," ignore them and any other advice they give.

Share this post


Link to post
Share on other sites
in some languages you would be highly restricted if there weren't any "goto"s

I see your point.

I'm curious about the intent behind the poll in this thread; are you meaning to form an opinion based on the general consensus?

The reason I made this poll is because i personally use them exactly this way:

I find them useful to do cleanup in functions that can fail, so I dont have to free all the resources everywhere, I can just goto the end, and put all the frees there

... and I got curious about whether people in general avoid them at all cost, or if there were indeed others who think they are the right solution in some (special) situations.

Share this post


Link to post
Share on other sites

I find them useful to do cleanup in functions that can fail, so I dont have to free all the resources everywhere, I can just goto the end, and put all the frees there


Isn't that more or less what exceptions are for?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!