Jump to content




Recent Status Updates

View All Updates

Binpress - Cut your development time and costs in half
Photo
- - - - -

Why is goto so bad?


  • Please log in to reply
16 replies to this topic

#1 Phoenixz

Phoenixz

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 222 posts

Posted 09 February 2010 - 02:23 PM

I realise goto is bad, so I don't use it, I try working around it with other loops though it'd be much simpler to use goto. So, I wonder why is it so bad? I believe it's neccesary to get a better understanding of C++.
  • 0
Posted Image

#2 dcs

dcs

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 730 posts

Posted 09 February 2010 - 02:44 PM

It's not bad, per se. It's just that over 99% of the time, the other loop constructs can be used to write cleaner, clearer, generally better code.

There are only rare exceptions: breaking out of nested loops or breaking a loop from a switch. Hmm. That's the only one from the top of my head at the moment. And the general better approach is to functionize this code.

[edit]There is an example in C99 that shows complex initialization code and the use of a goto to hop to the middle of a loop.

I try working around it with other loops though it'd be much simpler to use goto.

You probably just don't know how to use the other loop constructs very well.
  • 0

#3 whitey6993

whitey6993

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 156 posts

Posted 09 February 2010 - 02:56 PM

As dcs said, the goto loop can be used effectivly to write cleaner, time saving code, but only if it is used sparingly and in the right places. Otherwise, you end up with a bunch of spegetti code that almost no one can read unless it is HEAVILY commented (as all good programs should be ;)).

C++ does not implement the goto statement and it isn't presented in modern programming texts (I didn't even know C++ used goto until 6 months in). I believe early versions BASIC and variations on that language (such as Liberty Basic) rely on goto and implement it heavily, but other than that, I haven't seen a program with a goto loop in it in a long time.
  • 0

#4 agnl666

agnl666

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 172 posts
  • Programming Language:C, Java, C++
  • Learning:Python, Assembly

Posted 09 February 2010 - 02:59 PM

My CP 12 teacher gave a list of key words in Java today and then made a point of saying that "goto" is unused and considered bad programming to use. I believe i would be docked points if I used this statement.
He said that most programmers don't use it and when programs that still use it are obsolete it will be phased out of the language.
  • 0

#5 dcs

dcs

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 730 posts

Posted 09 February 2010 - 03:11 PM

The extreme approach ("never use goto") is generally spoken from ignorance, instructors included. It has its place, but in C and C++ it is rare.

I thought of another "common" use: shared cleanup code.
  • 0

#6 John

John

    CC Mentor

  • Moderator
  • 4,450 posts
  • Location:New York, NY

Posted 09 February 2010 - 07:07 PM

The mantra "never use a goto" is spoken from ignorance as dcs pointed out. Granted, it can easily be abused (just as object oriented programming), but it does have its place. In his book Code Complete, Steve McConell puts it best. See here for the excerpt I am referring to: Code Complete, First Edition
  • 1

#7 Feral

Feral

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 158 posts

Posted 10 February 2010 - 01:54 AM

In general people say "Never use goto" for the simply point that when most people start using it they never stop.

Unlike what whitey said goto does not create cleaner easier to read code, it is the complete opposite. A program using goto can be very complicate to follow.

As a personal rule of thumb I only use goto for debugging applications and in error checking. I would never use goto in a production piece, but this is more a personal feeling then a must.

The simple answer is goto has its place and its use but should only be used when it is necessary.
  • 0

#8 JCoder

JCoder

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 240 posts

Posted 10 February 2010 - 03:48 AM

Breaking from inner loops or swithces by goto in Java is not required - you have labeled break/continue for it. BTW: break and continue are only slightly less evil than goto. You should also use them sparingly (for the last few years I had to use break or continue maybe a few times - now my programs are completely break/continue/goto free).
  • 0

#9 WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderator
  • 16,934 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 10 February 2010 - 08:50 AM

There are two reasons to avoid goto:
1) it is unnecessary in most modern languages. Assembly is about the only language where its use is required that is in common use today.
2) liberal use of gotos makes it almost impossible to decipher/maintain code.
  • 0
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#10 alienkinetics

alienkinetics

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 144 posts

Posted 10 February 2010 - 10:11 AM

...I try working around it with other loops though it'd be much simpler to use goto.


Ok, Im now intrigued.

Can you post an example of using a goto that is simpler? Im not testing you or anything, im just interested.

Im guessing you are not writing enough functions which alow you to terminate a process with "return".
  • 0

#11 outsid3r

outsid3r

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 494 posts

Posted 10 February 2010 - 10:32 AM

Breaking from inner loops or swithces by goto in Java is not required - you have labeled break/continue for it. BTW: break and continue are only slightly less evil than goto. You should also use them sparingly (for the last few years I had to use break or continue maybe a few times - now my programs are completely break/continue/goto free).


In my opinion it's not a question of evilness but code readability. goto statement makes the code much less readable if it is used constantly, continue and break are very readable, but in my opinion there is nothing evil about 'goto' or 'continue' or 'break' statements.
  • 0

#12 Aereshaa

Aereshaa

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 638 posts

Posted 13 February 2010 - 12:41 PM

If you consider break and continue statements evil, what about returns in the middle of a function?
bool prime(int x){
 for(int i=1;i<sqrt(x);i++)
  if(x%i==0)return false;//you would consider this evil, no?
 return true;
}
Non-structured flow is necessary for many algorithms. It's not evil, just complex.
  • 0
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz




Powered by binpress