Use of labels and goto statements

Why are they soooo bad? Your views and comments!

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

43 Replies - 7796 Views - Last Post: 28 May 2012 - 08:24 AM

#1 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Use of labels and goto statements

Posted 25 January 2010 - 04:38 AM

Ever since the emergence of structured programming languages, the use of code labels and goto statements has been shunned by many programmers. Personally, I believe that label statements and the use of the goto statements can have a positive influence on not only the quality of the code produced, but also its readability and it's maintainability. Here is an example:-

while ( true )
{
	eof = read_file();
	if ( eof ) break;
	/* ... do something ... */
}
/* ... continue ... */



and here is the same example using labels and the goto statement

read_file_next_element:	eof = read_file();
							if ( eof ) goto read_file_eof;
							/* ... do something ... */
							goto read_file_next_element;
read_file_eof:		/* ... continue ... */



Notice that with meaningful label names, the code becomes self documenting (I say that rather loosely btw!).

The history behind the animosity toward labels and goto statements actually stems from the days when memory for compilation was so limited, programmers had little choice but to write the code as follows:

l1:	eof = read_file();
		if ( eof ) goto l2;
		/* ... do something ... */
		goto l1;
l2:	/* ... continue ... */



What are your thoughts on this rather explosive topic?

This post has been edited by Martyn.Rae: 25 January 2010 - 06:15 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Use of labels and goto statements

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: Use of labels and goto statements

Posted 25 January 2010 - 06:05 AM

read_file_next_element:	eof = read_file();
							if ( eof ) goto read_file_eof;
							/* ... do something ... */
read_file_end:		/* ... continue ... */
							goto read_file_next_element;



Notice how truly screwed you can get? I think we've beaten this horse to death.

Simply, there's nothing you need a goto for in procedural programming. It can be syntax sugar for certain jobs, but it comes with a diabetes warning. There's just no compelling reason to use it and it borders on incompetence to teach it to new programmers.
Was This Post Helpful? 0
  • +
  • -

#3 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Use of labels and goto statements

Posted 25 January 2010 - 02:38 PM

label: one

label: avgai on this GOTO one

label: I'm with ba GOTO avgai on this
Was This Post Helpful? 0
  • +
  • -

#4 TriggaMike  Icon User is offline

  • Using up all your 1's and 0's
  • member icon

Reputation: 85
  • View blog
  • Posts: 1,103
  • Joined: 26-September 08

Re: Use of labels and goto statements

Posted 25 January 2010 - 03:09 PM

Methods, Loops, and other structures replaced all these structures long ago. You might as well try to write a book with a stone tablet and chisel. There's a reason things have advanced.
Was This Post Helpful? 0
  • +
  • -

#5 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: Use of labels and goto statements

Posted 26 January 2010 - 02:31 PM

I don't really see how your second example is more readable.
Was This Post Helpful? 0
  • +
  • -

#6 SpectateSwamp  Icon User is offline

  • D.I.C Head

Reputation: 51
  • View blog
  • Posts: 243
  • Joined: 31-December 07

Re: Use of labels and goto statements

Posted 27 January 2010 - 08:12 AM

View PostMartyn.Rae, on 25 Jan, 2010 - 03:38 AM, said:

Ever since the emergence of structured programming languages, the use of code labels and goto statements has been shunned by many programmers. Personally, I believe that label statements and the use of the goto statements can have a positive influence on not only the quality of the code produced, but also its readability and it's maintainability. Here is an example:-

What are your thoughts on this rather explosive topic?


You are bang on here. So many things were done in the name of speed. I'd like to see them replace my Desktop Search with a program that can do the same functions. I created my own labels and use centralized error trapping. Everybody knows that an end of file is encountered during almost all the file reads. Why does it have to be there making the code less readable. My code has been changing for 10 years with no problems.
Was This Post Helpful? 1
  • +
  • -

#7 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Use of labels and goto statements

Posted 27 January 2010 - 11:10 AM

Basically I like to say that just because you can do something with code doesn't mean that you should. I started out with a spaghetti language, plain old BASIC. With all of the gotos and such it was hard to write a large program with out getting into a huge mess of code that was hard to maintain or change. Structured programming allowed us to create larger programs by being able to organize your code better and have reuse of code, not quite as easily done with spaghetti code. Object-oriented programming gave us the ability to create even larger projects by instead of code dictating data, data now dictates what code is run. You get a black box and the black box works properly with out you needing to know what is inside the black box.

Sure, you can write a program littered with gotos and such but the idea is to write responsible code that if you, a member of a team you are working on, or just about anybody has to come back and make changes to the code using well written code that it is easy to follow the flow of the logic of the program easy to follow. If you need to trace through your code following where each goto jumps to you will be wasting time and engergy.
Was This Post Helpful? 0
  • +
  • -

#8 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: Use of labels and goto statements

Posted 27 January 2010 - 11:22 AM

Try Assembly, you going to like it.
Was This Post Helpful? 0
  • +
  • -

#9 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1255
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: Use of labels and goto statements

Posted 28 January 2010 - 02:56 AM

Edsgar Dijkstra said that GOTO statements should be considered harmful so you better believe it since he is way smarter than any of us.

This post has been edited by Dark_Nexus: 28 January 2010 - 04:28 AM

Was This Post Helpful? 0
  • +
  • -

#10 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Use of labels and goto statements

Posted 28 January 2010 - 05:59 PM

View PostRaynes, on 25 Jan, 2010 - 03:38 PM, said:

label: one

label: avgai on this GOTO one

label: I'm with ba GOTO avgai on this


That was amazing. :D: :^:
Was This Post Helpful? 0
  • +
  • -

#11 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Use of labels and goto statements

Posted 28 January 2010 - 06:11 PM

View PostDark_Nexus, on 28 Jan, 2010 - 04:56 AM, said:

Edsgar Dijkstra said that GOTO statements should be considered harmful so you better believe it since he is way smarter than any of us.

Linus Torvalds said that GOTO statements should be considered helpful so you better believe it since he is way smarter than any of us.

(Sorry, couldn't resist :P. I actually agree with the whole "gotos are bad" mindset here)
Was This Post Helpful? 0
  • +
  • -

#12 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,023
  • Joined: 15-July 08

Re: Use of labels and goto statements

Posted 28 January 2010 - 06:23 PM

I think that both of these mens' points are valid and the line is very close. Linus Torvalds says that MOST OF THE TIME, one should not use GOTOs, but he also said that they should be used only in certain cases (read the article)
Was This Post Helpful? 0
  • +
  • -

#13 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Use of labels and goto statements

Posted 30 January 2010 - 11:12 AM

Nothing like a good goto debate.

I personally tend towards the middle on this. I don't think goto is liquid evil and once inserted into code all that it touches turns into a poorly debugged bag of device drivers...

However -- I think that goto should be used sparingly because it DOES create code whose flow is harder to read. I think that it should be used for its utilities rather than as a replacement for the standard control structures. In C/C++ goto can be useful when integrating with assembly language for example -- sure its not terribly readable -- but neither is inline assembly... in fact optimized code in general is usually a mess.

For standard every day situations it should be avoided, like the other "evils" of programming like global variables -- they have their uses, but these should be kept to a minimum.


Personally I also dislike infinite loops. I think: while(true) { } or its cousin the for(;;) { } should be avoided... unless you really DO want the loop to run indefinitely.
Was This Post Helpful? 0
  • +
  • -

#14 0xFF  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 12-December 09

Re: Use of labels and goto statements

Posted 30 January 2010 - 09:13 PM

I think the problem here is people read a soundbyte from Linus Torvalds about the GOTO, and think they know what he was saying.

I actually wrote my thoughts on this previously today, I'll post them up here paraphrased a bit...

me said:

Back when computer languages first came out there existed this thing called a "GO TO" statement. An example of this code with a goto would be along these lines.

Psudocode:

var x = 10
line2: //This would have been called a "label"
x ++;
if (x < 100) {
goto line2 //This tells the program to jump to "line2"
} else {
goto end_of_block //This tells the program to jump to "end_of_block"
}
end_of_block: //This is another label.
document.write(x);

This code would keep repeating the x++ command, and checking if it's value is under 100. At the end of this "goto loop," you will end up with x having a value of 100.

The use of the goto has lost it's place in modern programming. I am going to be bold here and say you can use the goto, and I'm going to invite the scorn of the last 50-years of instructors, authors, and a lot of the community just for software for saying this. The goto statement has it's uses, and many statements are themselves a goto (for example, the IF statement tells the computer to ignore a bunch of code if a condition is false (ie, goto after that code).

Modern and structured loops support it under a different name. All computer code is doing is simply storing information, adding or subtracting, and moving to a different instruction to do one of these previously mentioned tasks (learn some assembler to gain this understanding). A goto is the most basic form of this. The reason I say it has lost it's place in modern programming is because many people in the middle of the 1900s felt it was responsible for poorly written code. For example, in a 10,000 line program, you could jump from line 100, to line 8000. The two areas of the program could be un-related. This leads to very hard-to-read programs. Working with them (should you ever have to look at one with them, I feel your pain). Those who say it leads to messy code are technically wrong; it's not goto's fault that the programmer is using something the wrong way. You will see modern programming theory explicitly forbid the use of them. While you will never once need to use a goto, it's important to know what it is, how it works, and that it's not goto's fault that we have poorly written programs.

You design and code good programs, through understanding of structure, not putting the blame onto a single statement and saying your code is bad just for having them. This being said, I have never needed to use a goto in any software I have written, and neither will you. The uses for a goto are highly outdated -- optimizing speed of your program is dealt with mostly by the compiler/interpreter (most CS professionals will say this is where the task of optimization is). All logical flow controls are (when examined deeply) are the same as a goto.

Just to make myself clear on the goto, I will say this in summary:
The goto is not the reason we have poorly written programs.
We have poorly written programs because we have poor coders.
We may have a program void of any goto and still have a mess of a program.
A goto may have been used to improve speed and performance, but today's average computer is so powerful this doesn't make a large impact.
Most modern languages provide you with the tools to never have to use them.
You probably will use them less than 10 times in your life.
You will not be using them early in your programming career. You need to respect your limitations as a new developer (not a code monkey who thinks they know everything and starts to arrogantly throw statements around).
We shouldn't be removing power from a language to allow for more programmers with less knowledge.

Was This Post Helpful? 0
  • +
  • -

#15 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Use of labels and goto statements

Posted 31 January 2010 - 12:57 PM

me said:

Back when computer languages first came out there existed this thing called a "GO TO" statement. An example of this code with a goto would be along these lines.

Psudocode:

var x = 10
line2: //This would have been called a "label"
x ++;
if (x < 100) {
goto line2 //This tells the program to jump to "line2"
} else {
goto end_of_block //This tells the program to jump to "end_of_block"
}
end_of_block: //This is another label.
document.write(x);

This code would keep repeating the x++ command, and checking if it's value is under 100. At the end of this "goto loop," you will end up with x having a value of 100.


Let's tidy this up a bit!

					int x = 10;
start_of_loop:		x++;
					if ( x < 100 ) goto start_of_loop;
end_of_block:		document.write(x);



(Soz 0xFF)

As professional programmers, we should always be striving for perfection in the code we write, regardless of crazy timescales devised by people who think they know what they are talking about. I have seen truely horrific code in C and C++ produced without the use of a single goto or label.

Most of you stating that goto's should never be used as they have been superceeded by structured statements have obviously never tried to maintain structured code that is poorly written. It is not fun trying to wade through thousands of lines of uncommented code (because they heard from a prominent person that structured code was self-documenting), looking for a single line fix.

By the same token, I am not suggesting that goto's are a solution either. My original post stated:-

"I believe that label statements and the use of the goto statements can have a positive influence on not only the quality of the code produced, but also its readability and it's maintainability."

A screwdriver is a useful implement and has purpose. It's purpose is to rotate a screw in either a clockwise or counter-clockwise direction. That being said, why do so many people use a screwdriver as a crowbar, a hammer and a murder weapon?

The poor goto did not ask to exist in the world of structured and object-oriented programming languages but it does. We as responsible programmers should know when to use it and when not to!
Was This Post Helpful? 2
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3