The Infamous Goto

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Ion Dune
Posts: 453
Joined: Mon Nov 12, 2007 8:29 pm
Location: California, USA
Contact:

The Infamous Goto

Post by Ion Dune »

I've heard a lot that using Goto is bad. But how bad is it? I'm adding a main menu to my game, and though I've got it working with a 'while', its complicated and I'd really like to do something like this:

Code: Select all

menu:
while (menu)
{...}

while(game)
{...}

goto menu;
Would this be so bad?

Thanks for your time.
Swarmer
Posts: 100
Joined: Mon Apr 16, 2007 7:23 am

Post by Swarmer »

It would be so much cleaner to just have the menu and game as state objects, and then just call menu->run() or game->run() whenever you need to.
The main problem with goto is that it makes code crazy and hard to read. If you use it in a few clear places and keep the structure very intuitive and readable, then I think that you should feel free to use it.
pelonzudo
Posts: 20
Joined: Wed May 07, 2008 11:14 am

Post by pelonzudo »

Code: Select all


do{
//menu here
...
...
...

//here you call the game start from the menu option
}while(!salir)


void gameStrart() {
   while(game->run()){
   ..
   ..
   ..
   }
}
That is a "cleaner" way, isn't it?
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

You will hear lot of things to be bad while you will learn C++. True is, they are bad mostly in some context. They might be good in other. Look at goto as a tool on your disposal ...use your own brain and opinion on if and when to use it or not.

You will hear global variables to be bad too for example. There is nothing bad on it however and in some cases globals might be the most efficient way to go. Of course they might be bad in other cases.

So my advice is: Do not accept opinions of others without consideration. Do not do it even if they seem much more experienced than you.

In programing, good is what leads you to your goal, preferably the most easiest and efficient way. Bad is what prevents you from getting there.
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

arras wrote:You will hear lot of things to be bad while you will learn C++. True is, they are bad mostly in some context. They might be good in other. Look at goto as a tool on your disposal ...use your own brain and opinion on if and when to use it or not.

You will hear global variables to be bad too for example. There is nothing bad on it however and in some cases globals might be the most efficient way to go. Of course they might be bad in other cases.

So my advice is: Do not accept opinions of others without consideration. Do not do it even if they seem much more experienced than you.

In programing, good is what leads you to your goal, preferably the most easiest and efficient way. Bad is what prevents you from getting there.
Amen.

In the case of goto, it's the recommended way to break out of nested for loops, and for jumping to other switch statements.
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
Zeuss
Posts: 114
Joined: Mon Nov 08, 2004 9:02 pm
Location: Canberra - Australia
Contact:

Post by Zeuss »

Jumping out of nested loops!!!

Huzzah, for some reason I never thought of using a goto for that, probably because it has been ingrained in me to not use them.

Yay for the forums, learn something new every day.

Saying that though, really you could make your nested loop inside a function and use the return function to break out of it, and still avoid goto.
Help make Irrlicht even Better! Create and submit your own Irrlicht Extension
Want a Games Education? Try The Academy of Interactive Entertainment
Swarmer
Posts: 100
Joined: Mon Apr 16, 2007 7:23 am

Post by Swarmer »

Even then, it's probably best to use break or continue in loops.
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Yes but when you are going out of the way to create separate functions or temporary boolean variables to break out of several loops at once when its inconvenient, thats being a little too pedantic. I mean its not like *gets eaten by dinosaur*
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

As long as you're sure that all the necessary destructors or constructors are called, your values are all initialized, etc. But using a different approach (which might require some additional bools to set some states) seems far more tractable and gives the compiler some additional confidence in the code to allow optimizations.
Post Reply