C++ - Is Goto a Good Practice?

Started by
45 comments, last by SiCrane 11 years, 5 months ago
Stay away from the evil goto statement in C++. it may seem nice until your project gets larger at which point they'll help to create 'spaghetti code'. C++ is made so that you don't have to use them. The only time you should need to use goto statements in in Assembly.
Advertisement
goto still has its place in C (take a look at the Linux kernel for lots of examples of non-destructive goto use), but for me there has never been any sensible reason to use it in C++.
IMHO the goto statement should not be used, but there are some few exceptional cases, for example if you are inside a nested-nested-nested loop than you might use it. There are other ways though ( better in my opinion ) .

I link you this http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html that was written by dijkstra about the use of GOTO statement. It's a good read!
[font=arial,helvetica,sans-serif]This is not really related to your question, but I think you may be interested to recieve other feedbacks on your code.[/font]

[source lang="cpp"]C.health < 0 || C.health == 0[/source]
The code above is equivalent to
[source lang="java"]C.health <= 0[/source]
What's M in your code? You have used it, but it is not declared in the function. If it is some kind of global or member function I think you should probably use a more descriptive name.
Also, please put brackets around logical conditions!
[source lang="cpp"]((a < 0) || (b == 0))[/source]
Yes, I know, operator precedence generally plays in your favor, but it just feels so much more readable to me (provided you don't end up with five or six nested parentheses, but in that case you probably should simplify the condition anyway). It's more a matter of personal taste, thus subjective, but I thought I'd throw it out there.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”


I only used goto in one small section, since I don't have much need for it elsewhere.
[/quote]
You are essentially abusing goto to create a loop. Instead, use an explicit loop:

void Combat::combatChoice(Character& C) {
if (C.health < 0 || C.health == 0) {
cout << "----------------- You died... ------------------" << endl;
cout << "Oh dear, it seems you have died... Game Over." << endl;
return;
}

bool action = false;
while(!action) {
cout << "----------------- Battle Options ------------------" << endl;
C.Display();
cout << "What do you want to do? \n"
<< "Type the number of the action and press enter. \n"
<< "[1] Melee Attack \n"
<< "[2] Gun Attack \n"
<< "[3] Health Potion" << endl;

short choice;
cin >> choice;
switch (choice) {
case 1:
cout << "\n----------------- Battle Results ------------------" << endl;
C.meleeAttack(M);
action = true;
break;

case 2:
cout << "\n----------------- Battle Results ------------------" << endl;
if(C.ammo == 0) {
cout << "You're out of ammo! \n" << endl;
} else {
C.gunAttack(M);
action = true;
}
break;

case 3:
cout << "\n----------------- Battle Results ------------------" << endl;
if(C.potions == 0) {
cout << "You're out of potions! \n" << endl;
} else {
C.useHP();
action = true;
}
break;
}
}
}

Note that this automatically handles the case where the user types an invalid option by looping around, though it is generally nicer to print a specific message in this case.

Another potential improvement is to dynamically remove invalid options from the menu. Something like this:

#include <string>
#include <vector>
struct CombatAction {
typedef void (*ActionFunctionPtr)(Character &character, Monster &monster);
string name;
ActionFunctionPtr action;

CombatAction(const string &name, ActionFunctionPtr action)
:
name(name),
action(action)
{
}
};
void punch(Character &character, Monster &monster) {
character.meleeAttack(monster);
}
void shoot(Character &character, Monster &monster) {
character.gunAttack(monster);
}
void heal(Character &character, Monster & /* unusued */) {
character.useHP();
}
void Combat::combatChoice(Character& C) {
if (C.health < 0 || C.health == 0) {
cout << "----------------- You died... ------------------" << endl;
cout << "Oh dear, it seems you have died... Game Over." << endl;
return;
}

vector<CombatAction> actions;
actions.push_back(CombatAction("Melee Attack", &punch));
if(c.ammo > 0) {
actions.push_back(CombatAction("Gun Attack", &shoot));
}
if(c.potions > 0) {
actions.push_back(CombatAction("Health Potion", &heal);
}

bool action = false;
while(!action) {
cout << "----------------- Battle Options ------------------" << endl;
C.Display();
cout << "What do you want to do? \n";
cout << "Type the number of the action and press enter. \n";
for(size_t i = 0 ; i < actions.size() ; ++i) {
cout << "[" << (i + 1) << "] " << actions.name << '\n';
}

size_t choice;
cin >> choice;
if(choice > 0 && choice <= actions.size()) {
cout << "\n----------------- Battle Results ------------------" << endl;
actions[choice - 1].action(C, M);
action = true;
} else {
cout << "Invalid option!" << endl;
}
}
}

I'm not sure if you are familiar with std::vector or function pointers. Feel free to ask a question if you don't understand what this code is doing.

Note: I haven't compiled or tested any of the code in this post, so caveat emptor.
My Teacher always told me that GOTO is forbidden.

Follow my hobby projects:

Ognarion Commander (Java/LIBGDX): https://github.com/OlafVanSchlacht/ognarion-commander

If you are posting in beginners forum then you shouldn't use goto in your code wink.png
Once you reach advanced level... well, then you know yourself.
Lauris Kaplinski

First technology demo of my game Shinya is out: http://lauris.kaplinski.com/shinya
Khayyam 3D - a freeware poser and scene builder application: http://khayyam.kaplinski.com/

If you are posting in beginners forum then you shouldn't use goto in your code wink.png
Once you reach advanced level... well, then you know yourself.


...then you'll rather use comefrom (http://en.wikipedia.org/wiki/Comefrom)

openwar - the real-time tactical war-game platform

When writing C, I use goto quite a lot for error handling and cleanup before returning an error code (rather than writing repetitive code after each failure check).

For those who are convinced that goto is bad in every single situation, have a look at the OpenBSD source code (which is renowned for good code quality) and you will see that goto is used very often.

For C++ however, I find that with RAII (and smart pointers), if you just throw an exception (or even return an error code) then all memory is cleaned up anyway.
http://tinyurl.com/shewonyay - Thanks so much for those who voted on my GF's Competition Cosplay Entry for Cosplayzine. She won! I owe you all beers :)

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.

This topic is closed to new replies.

Advertisement