FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Why do people hate goto statements so much?





shashwatblack
Sure there can be other ways to do the work to avoid using goto, introducing a new loop, or a flag, or other ways. But why avoid a feature that is built for that exact purpose?
You could use a statement like,
Code:
switch (condition)
{
    case true:
        statements1;
        break;
    case false:
        statements2;
        break;
    default:
}


in place of

Code:
if (condition)
{
    statements1;
}
else
{
    statements2;
}


but you use if statements.. because it is built for that right? and the code becomes easier to read.
So why avoid goto, and write a ugly code that is both difficult to read and has to have more of everything - codes, variables, loops.
I understand the cases about goto not limiting its scope and giving rise to errors such as skipping variable declarations, memory leak problems, but we can easily avoid these by taking appropriate measures, which a programmer must do. the error is not due to the goto statement, it is due to the programmer.
Also there can be cases where goto makes the code really easy, for example today i was working on a project and came across a situation and managed it using goto;

Code:
//variable declarations
int occurrence = 3;
int occr = 1;
label:
//variable assignments
//inside lots of codes and loops
    if (succeed && occr == ocurrence)
        return;
    else
        {
        occr++;
        goto label;
        }
//out of loops


sure i could have used a flag and checked it at the end of every loop, and finally break out of the outermost loop, and return to the label by putting all the code in yet another loop. but that would make it very ugly, and difficult.
what is your say on this? why is goto despised so much?
codegeek
I don't hate the goto statement, but I steer away from it as far as possible. I once read that the goto statement is comparable to a double-edged knife with no handle. Sure, you can use it occasionally and with great effect, perhaps you can store it easily because it has no handle, you might even learnt to do some tricks with it and be thinking about adopting it full time and dispensing with your regular knife.

But the truth of the matter is that if you make a habit of it, it's gonna cut you someday, and that is guaranteed. Yeah, even a regular knife will cut you if you handle it badly, but when you are in a high pressure situation and you have to use a knife efficiently in the least amount of time, which one would you choose: the one which has a high probability of cutting you or the regular knife which everyone else uses?
Marcuzzo
usually because you end up writing spaghetti code.
the idea is to keep your code as clean and maintainable as possible.
this means keeping your code blocks as short as possible.

if else statements and switches have nothing to go with 'GOTO' but are incorrectly used by unexperienced programmers.
have a look at the following:
Code:
if ( statement)
{
      somemethod();
      anotherMethod();
}
else
{
     badabing();
     badaboom();
}

There isn't anything wrong with this snippet, except when these functions only hold a couple of statements and maybe even call another function.
you can see where this is going to if you keep jumping from function to function, in the end you can't even get your head around a few couple hundred lines of code, let alone a project of a couple of thousand of lines.

in your example:
shashwatblack wrote:
Code:
switch (condition)
{
    case true:
        statements1;
        break;
    case false:
        statements2;
        break;
    default:
}

this is pure overkill, a condition will always return a boolean value, so your condition is either true or false.
no matter what anybody says, this should always be coded as:
Code:
if ( condition )
{
 // condition true
}
else // condition false
{
 ...
}

you only use a switch if you have more then 2 options:
Code:
int myInt = 2;
switch ( myInt)
{
   case 1:
      .....
      break;
   case 2:
      .....
      break;
   case 3:
      .....
      break;
   default:
      ...
      break;
}

in my projects I stick to the following rules:
1. CLEAR and SIMPLE Variable naming, not just for me but for anybody.
2. clear conditions: anybody should be able to tell immediately what is being checked
3. Loops are only ended when the condition of the loop is met. so no ExitLoop or Break... ever

Everybody has a different coding style but it mostly comes down to the same thing, keep your code clean and maintainable, avoid spaghetti code and make sure that other people don't have to analyze your entire code before they know what is going on.

try working in a project team and you will learn


EDIT: I just want to add the following, most of the projects I've seen that had a lot of "goto's" in them and where functions were flying all over the place, were projects that have had little or no analysis and planning.
if you work out your project before you write a single line of code you will have less of these (what I like to call) jumpers
shashwatblack
my point with the if else and switch was that you could use switch for such a case, but you don't.. because it just doesn't make any sense. i wouldn't use goto too much in a program either, like intentionally putting a goto even where the operation can easily be handled in other ways. but you do come across situations where using a single goto makes it a lot easier to read the code.. i wouldn't jump a thousand lines, because surely that would be very error-prone. but a couple of hundred lines, why not?
goto is still a part of the language not for nothing. not for being thought of as something to be despised. there are good uses of goto, and people should learn to implement them, and appreciate them.
shashwatblack
and codegeek, i really like the analogy. a double edged knife with no handle. but it isn't that dangerous, when you take proper care. and don't you agree? that it makes the code easier to comprehend in many situations?
sure a habit would be unfruitful, but using it once in a while to avoid making a complex mess of loops and flag checkings wouldn't hurt.. Smile
Arrogant
Why is goto such a big deal to everyone
When you can code something without using it then why use it?
It just opens ways to errors and memory leakage
Stay_Classy
The goto statement was rather useful at one point, as it allowed for simple "jumping" inside a program. However, functions do this MUCH more efficiently, and also return execution to where they were called, simplifying this process. Sure, you can do
Code:

:thing1
goto thing2

:thing2
goto thing1


but why not do (this will be in Python instead of batch)

Code:

def thing2():
    return True

thing2()


So yes, it does make things easier sometimes, but in modern cases, functions work much better.
Marcuzzo
I want to add this to this topic: if your code is readable, manageable and it doesn't jump up and down through your code files then you are doing the right thing.

Like Pablo Francisco used to say: "GOTO is like a girl with beautiful body but with an ugly face, 'I love Spaghetti, but I've got shit on it'"
Laughing

And on that bombshell, goodnight Frihost, I'm wasted
Possum
No matter what language (pascal fro example) it is recommended not to use the goto statement.. But my thoughts are if it works.. use it..!!
Mr_Howl
Here's a good read regarding GOTO. It's a discussion with Linus Torvalds, the creator of Linux.

Using goto in Kernel Code

The main take-away of the conversation, for those who don't feel like reading it, is that goto is not inherently evil and can be very useful, even making code cleaner in some places.

Linus also seems to disagree with Marcuzzo's feelings about break statements in loops:

Quote:
The Pascal language is a prime example of the latter problem. Because it doesn't have a "break" statement, loops in (traditional) Pascal end up often looking like total ****, because you have to add totally arbitrary logic to say "I'm done now".
Possum
Quote:
Linus : I think goto's are fine, and they are often more readable than large
amounts of indentation.


Uses that magic word.. readability
metalfreek
I really don't hate goto statement but without a careful use it might be a source of logical error which is damn hard to find.
friend2012
I too hate goto as it make code updation tedious and time consuming when you return to the code after few months. Also goto may cause memory leakes and slow processing speed which can effect code performance in real situations.

Use of conditional statement(if.. then.. else.. or swith case) are better then goto.
Bikerman
It encourages non-top-down programming - something usually called 'spaghetti code' because it is a tangle of flow lines jumping all over the code itself..
Professional coders frequently work with code that is not their owm - always problematic unless the coder was meticulous with the comments and structure - but there are few things worse than working with some legacy code, written by some long forgotten BASIC Bandit, which jumps here, there and everybloodywhere. Makes my stomach churn to think of it......
bellalines
The goto statement in the original snippet was clear and self explanatory.

Imagine the same statement in a 10,000 line application with 10 levels of indentation.

This makes the code impossible to debug, fix or read. Therefore they are considered bad practice (and downright annoying).
Arrogant
For me, it's because

    It breaks that smooth execution flow.

    Debugging is very hard if you come up with errors.

    It's really hard to implement in a big program.
windhw
I don't think goto statement is useless everywhere. It's excellent in Error Handing.
Nyasro
I dont think goto statement is good for programming,
if it was good every programmer would have used it.
Peterssidan
windhw wrote:
I don't think goto statement is useless everywhere. It's excellent in Error Handing.

Nowadays exceptions are normally used instead.
boinsterman
I first learned about GOTO as used in BASIC and Pascal. Circa 1988, the professor teaching my Pascal course told us that the overuse of labels could get a programmer fired. Apparently, some people had intentionally made their code so complicated that if they were fired the company would be unable to efficiently make changes in their code because of not being able to map the GOTO statements and labels effectively without that person. When the computers themselves were expensive, it was worth it to keep such people around. But as of that time, "what's valuable now is programmer time", meaning the programmer's pay. So someone who did that a lot would be fired.
Related topics
RAP MUZIK
The Death of lol
issues you hate
Do you hate ads on TV?
A little help on C++
NAZIS
Howcome the universe is larger than it's old?
Most hated man in the world - who?
Google copied Microsoft ...!!!
Wath should my new PC contain
Why people hate Mac-OS (users)?
I conquered my body, can you?
The hate for the existing GOD...
NBA Finals: I Want A Lakers -- Celtics Showdown!
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.