Use of labels and goto statements

Why are they soooo bad? Your views and comments!

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

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

#16 alentius  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 21
  • Joined: 04-October 09

Re: Use of labels and goto statements

Posted 31 January 2010 - 01:42 PM

http://xkcd.com/292/

Sorry, couldn't resist.
Was This Post Helpful? 0
  • +
  • -

#17 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 - 02:09 PM

@alentius :) excellent!!!!
Was This Post Helpful? 0
  • +
  • -

#18 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4334
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: Use of labels and goto statements

Posted 31 January 2010 - 02:33 PM

Can a goto label be useful? Sure. Can they do things that regular code without code labels can do? No. Are labels overly abused in many situations? Most definitely. If code labels can be, and often are, abused and they can't do anything that code without labels can do, then there is no reason to use them. I think their removal wasn't about code functionality so much as removing the chance of their abuse.

You might be able to write good code with labels, but for every one of you there are a 100 out there using them to destroy program flow. I rather remove a feature like this and save everyone time from having to debug bad goto label use than keeping it in just so a select few can use them to do something that can be done without them anyways.

I have yet to see any example where a label use is more appealing than the alternative no label use. Even the OP's example is still not convincing enough for me.

:)
Was This Post Helpful? 0
  • +
  • -

#19 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,685
  • Joined: 16-October 07

Re: Use of labels and goto statements

Posted 31 January 2010 - 03:24 PM

int x = 10;
for(; x<100; x++) { /* loop */ }
document.write(x);



View PostMartyn.Rae, on 31 Jan, 2010 - 01:57 PM, said:

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.


Agreed.

View PostMartyn.Rae, on 31 Jan, 2010 - 01:57 PM, said:

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.


Quite. You don't "maintain structured code that is poorly written", you fix it.

View PostMartyn.Rae, on 31 Jan, 2010 - 01:57 PM, said:

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.


Agreed.

I still fail to see any positive case being made for the use of goto here. Crap code is crap, goto or no goto. Adding a goto to crap merely increases it's crappiness. Or, worse, allows a further obfuscating of the underling problem.

View PostMartyn.Rae, on 31 Jan, 2010 - 01:57 PM, said:

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!


The point is, most responsible programmers do know when to use it... never. ;)
Was This Post Helpful? 0
  • +
  • -

#20 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 31 January 2010 - 04:42 PM

Quote

Adding a goto to crap merely increases it's crappiness. Or, worse, allows a further obfuscating of the underling problem.


I disagree. There are cases when you may end up adding in extra flags and conditions within code to trigger an exit of n'th level of loops. Realistically, break and continue are also a goto, which are mostly considered "fine" to use.

Again, it's not because of the goto that we have crap code. It's never been because of the goto that we have crap code. Removing the goto from standard programming has perhaps made crap code "less messy," but these monkeys still find ways to make massive mess of things.

Banning a goto because people haven't been taught how to use them is absurd-- it's like banning erasers from school kids because they throw them at each other.


For reference sake, this is the wrong use of a goto..
Was This Post Helpful? 0
  • +
  • -

#21 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,685
  • Joined: 16-October 07

Re: Use of labels and goto statements

Posted 31 January 2010 - 05:12 PM

Horse won't die! :P

Please show me code where a goto is a valid, and preferred, choice. For this in particular, show me code that's flawed where a goto is a sane fix to the problem.

Please, give me an example. Make me understand why you believe as you do. I am absolutely serious.
Was This Post Helpful? 0
  • +
  • -

#22 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 31 January 2010 - 07:22 PM

loop { {code we wanted to execute}
	loop { {code we wanted to execute}
		loop { {code we wanted to execute}
			loop { {code we wanted to execute}
				if (x==12) {goto exit}
			}
		}
	}
}
exit:




instead of say... being brainwashed by popular consensus to believe break isn't a goto.

loop {
	loop {
		loop {
			loop { if (condition) {x==12}
				if (x==12) {break}
				}
			if (x==12) {break}
			}
		if (x==12) {break}
	}
	if (x==12) {break}
}
exit:


or even in some obscure language that doesn't support break, or you insist that a loop must preform all cycles and be contained within the condition set.
while ((condition) && a!=1) {
	while ((condition) && a!=1) {
		while ((condition) && a!=1) {
			while ((condition) && a!=1) {
				if (x=12) {a=1}
			}
			if (a !=1) {code we wanted to execute}
		}
		if (a !=1) {code we wanted to execute}
	}
	if (a !=1) {code we wanted to execute}
}



Gotos lead to messy programs when you do crap like this:

print hello
var x = 10;
var y = "my name"
goto 100
b:
if x+y != georcey 900, goto c
.......
....

c:
.....

100:
print x + y
goto b



That's an example of how we ended up with big abortions of programs. We're not immune from them now, and we're not immune from them because java does not like pointers, and we're not immune from them in the world of OOP. Saying we should remove them because new CS students can't figure it out is entirely asinine.


If you've ever done any work with assembly you would probably be agreeing with me on this.
http://en.wikibooks....ly/Control_Flow

And again to reiterate myself, I'm not saying we should be using the goto more than ever, if you look at my first post in this thread I flat out say it's lost it's usefulness.

This post has been edited by 0xFF: 01 February 2010 - 05:14 PM

Was This Post Helpful? 0
  • +
  • -

#23 GenHornet18  Icon User is offline

  • Darken the Radar

Reputation: 36
  • View blog
  • Posts: 629
  • Joined: 19-October 09

Re: Use of labels and goto statements

Posted 31 January 2010 - 08:39 PM

Nice, I'm liking your use of the code tags ;)

He does make a valid point however, if you go back to your base language of Assembly (most of them) are forced to make use of a 'jump' statement (almost equivalent to 'goto'). I shouldn't say forced actually, as I've seen some perfectly workable code that doesn't make use of a 'jump' statement. But this is the base, and most languages are built on this. So whether you use it directly or it's being compiled into this for you it is most likely being used.

Main point I believe will be found here eventually, is that code (commonly referred to as spaghetti code) which uses many 'goto' statements is difficult to read and as an extension of this is therefore harder to debug. They are being used, but it doesn't have to be in a HLL where they aren't necessary and only make readability worse. The main point of programming in a HLL is to improve readability, the unfortunate fact is that the 'goto' statement does no excel in this area.

We moved up from machine language to improve readability and we can move up from the use of a 'goto' statement.


Side Note: I can swear this is the tenth (or better) topic with this exact same question (and pretty much, same responses). I vote for a topic merge (mod?).
Was This Post Helpful? 0
  • +
  • -

#24 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 01 February 2010 - 05:04 PM

View PostGenHornet18, on 31 Jan, 2010 - 09:39 PM, said:

Nice, I'm liking your use of the code tags ;)

I'll FIFY in a moment.

View PostGenHornet18, on 31 Jan, 2010 - 09:39 PM, said:

Main point I believe will be found here eventually, is that code (commonly referred to as spaghetti code) which uses many 'goto' statements is difficult to read and as an extension of this is therefore harder to debug. They are being used, but it doesn't have to be in a HLL where they aren't necessary and only make readability worse. The main point of programming in a HLL is to improve readability, the unfortunate fact is that the 'goto' statement does no excel in this area.

We moved up from machine language to improve readability and we can move up from the use of a 'goto' statement.

I agree that spaghetti code is hard to read, and I agree that abuse of the goto is undoubtedly the largest contributor to this, but neither of these are a reason to remove it or forbid the use of them. People abuse fast food, lets ban that too (a stretch, I know, but you understand what I'm saying). Instead of adding more training wheels to newbs, how about we federate coders who understand what they're doing instead of what we're growing into more and more each day: "I cut this from the net, I added the three statements I know to make it kind of work, it now has some bugs, help I don't understand how to use pointers."

View PostGenHornet18, on 31 Jan, 2010 - 09:39 PM, said:

Side Note: I can swear this is the tenth (or better) topic with this exact same question (and pretty much, same responses). I vote for a topic merge (mod?).

It's a big debate, on one side you have idiots like me that insist the goto should have ever existed, then you have the majority rule repeating what their teachers learned from some papers in the mid 1900s. I might make my next series of posts in this topic rebuffing their applicability.
Was This Post Helpful? 0
  • +
  • -

#25 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,685
  • Joined: 16-October 07

Re: Use of labels and goto statements

Posted 01 February 2010 - 05:46 PM

View Post0xFF, on 31 Jan, 2010 - 08:22 PM, said:

loop {
	loop {
		loop {
			loop { if (condition) {x==12}
				if (x==12) {break}
				}
			if (x==12) {break}
			}
		if (x==12) {break}
	}
	if (x==12) {break}
}
exit:



:blink: Goto's lead to programmers who can't think procedurally? Yep, I'll buy that. ;)

function lotsOfNestedLoops() {
	loop { {code we wanted to execute}
		loop { {code we wanted to execute}
			loop { {code we wanted to execute}
				loop { {code we wanted to execute}
					if (x==12) {return; }
				}
			}
		}
	}
}

lotsOfNestedLoops();
exit();


Was This Post Helpful? 0
  • +
  • -

#26 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 01 February 2010 - 05:54 PM

View Postbaavgai, on 1 Feb, 2010 - 06:46 PM, said:

View Post0xFF, on 31 Jan, 2010 - 08:22 PM, said:

loop {
	loop {
		loop {
			loop { if (condition) {x==12}
				if (x==12) {break}
				}
			if (x==12) {break}
			}
		if (x==12) {break}
	}
	if (x==12) {break}
}
exit:



:blink: Goto's lead to programmers who can't think procedurally? Yep, I'll buy that. ;)

function lotsOfNestedLoops() {
	loop { {code we wanted to execute}
		loop { {code we wanted to execute}
			loop { {code we wanted to execute}
				loop { {code we wanted to execute}
					if (x==12) {return; }
				}
			}
		}
	}
}

lotsOfNestedLoops();
exit();


Why did you make a function for something I need once? While we're at it lets make a function for everything.

You ended up using a return as a goto. What is this farce? I dare you to flowchar those two and show me how both do not look like a goto.

This post has been edited by 0xFF: 01 February 2010 - 05:57 PM

Was This Post Helpful? 0
  • +
  • -

#27 dorknexus  Icon User is offline

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

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

Re: Use of labels and goto statements

Posted 01 February 2010 - 05:56 PM

Considering one of the purposes of high-level languages is to increase program readability I would say go ahead and use GOTO as long as it doesn't detract from readability. However, I am also going to say that about 99% of the time the GOTO statement will detract from readability.

Quote

You ended up using a return as a goto. What is this face?


According to your logic any sort of flow control mechanism is a GOTO which is simply not true. If's, return's, switches, breaks, continues are all handled by the compiler differently and manipulate the stack differently.

This post has been edited by Dark_Nexus: 01 February 2010 - 05:56 PM

Was This Post Helpful? 0
  • +
  • -

#28 dorknexus  Icon User is offline

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

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

Re: Use of labels and goto statements

Posted 01 February 2010 - 06:11 PM

You could also re-write programs in assembly and they would be the exact same, but they are also harder to read. The difference between break, continue, return, and GOTO is that you have no idea where GOTO is going to jump to without hunting down the label. Sure it's easy to see in a 10 line function, but what about a much larger function like a process scheduler form the linux kernel? That's going to be a pain in the ass to be zooming all over the function trying to figure out which labels go where in a 3000+ line function.

On top of that it could be dangerous to just randomly jump to some portion of a function if you don't fully understand the dangers of GOTO. GOTO's can also make it impossible for compiler's to run proper optimizations on code because it makes it difficult fro the compilers to ensure safety across internal blocks.

The point is, the flow control constructs built into most modern languages handle these dangers for you and make programs safer and more robust at run time. They also add structure and readability to your source to make it easier to debug and maintain. GOTO defeats the purpose of using a modern language because it strips away those nice features and shifts the workload to the programmer to ensure that the jumps are safe.

In remote cases they are just fine to use and might actually make that small portion of code easier to read. You're right, we can't just completely demonize the use of GOTO but it's sorta like trying to program procedurally in a functional language. Why fight the structured paradigm if you don't have to?

This post has been edited by Dark_Nexus: 01 February 2010 - 06:13 PM

Was This Post Helpful? 0
  • +
  • -

#29 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,685
  • Joined: 16-October 07

Re: Use of labels and goto statements

Posted 01 February 2010 - 06:12 PM

View Post0xFF, on 1 Feb, 2010 - 06:54 PM, said:

Why did you make a function for something I need once?

Did I make the function more than once? I'm failing to see your point.

View Post0xFF, on 1 Feb, 2010 - 06:54 PM, said:

While we're at it lets make a function for everything.

That's the spirit! Then we could pass parameters, reuse code, and explicitly understand our entry and exit points. It's a party!

View Post0xFF, on 1 Feb, 2010 - 06:54 PM, said:

You ended up using a return as a goto. What is this farce?

It's called programming. It's generally considered good form, actually.

View Post0xFF, on 1 Feb, 2010 - 06:54 PM, said:

I dare you to flowchar those two and show me how both do not look like a goto.


A goto can jump anywhere. A return from a function can only go back to the point from which is was called. In this fashion, functions offer encapsulation and predictable behavior; which goto does not.

I don't really care if, at the assembly level, they all resolve to a bunch of jumps. That's not the point. The point is, in a high level language, I can control the flow of my program better than that. To not do so verges on the irresponsible.
Was This Post Helpful? 0
  • +
  • -

#30 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 02 February 2010 - 04:13 AM

Quote

You could also re-write programs in assembly and they would be the exact same, but they are also harder to read.


I don't find assembler programs harder to read!!! I actually find them easier :P

There again, I was brought up in a time where assembler was essential!! The first mainframe I worked on had 13624 24-bit words of user memory. This machine was mostly programmed in COBOL where you also had to use goto statements. Structured programming did not exist back then. We as programmers managed exceedingly well. The problem today is that there are so many people who have never seen a properly written program using nothing but goto statements. Hmmmm ... maybe I should write one and post it here.

Structured and object oriented programming has for the most part removed the need for the goto statement. It was deemed necessary by the designers of computer languages to be way to advanced for most people and back then the industry needed programmers. I had to sign indentures for four years as a trainee programmer before I could become a junior programmer. After two years as a junior programmer you would make programmer II, after two more years programmer I and two years after that senior programmer. These timescales were the industry standard!!! That was six years to be deemed to be a competant programmer ... many did not make the grade.

With such a lead time for the training and development of programmers, any mechanism to speed up this process was embraced by the industry. Unfortunately, the lead time in the production of programmers has been reduced, but at an awful cost. Let me explain.

I think those of you reading this that have considerable years developing software under their belt, will agree with me when I say you have to have an ability to break problems up into smaller chucks, ensure that the interfaces between those smaller chunks will operate/cooperate sucessfully to produce the desired result i.e.analysis and design. Understanding what the problem is, is in itself a sizeable task. The more experience you have, the more pertinent the questions will be providing a more accurate picture of the problem. All of this before fingers have started tapping away at the keyboard.

Having the right mindset is the key here, and that is not something you can teach particularly well. Shunning the goto's use in software development is not the solution to the problem. The problem is not all programmers make good programmers - and the industry needs good programmers. This is not a fault of the language constructs but more a reflection on the people using those constructs.

If you do not have the ability to understand how to write quality software using goto statements, then how can you understand how to write quality software without? This is not an attack on any individual, but a generalisation. How can you be deemed to be a senior programmer with two years of experience?

@baavgai, you said that the goto should not be taught to beginners (least that's what I think you said ... apologies if it wasn't!). I feel that not only should it, but beginners should be forced to write software with it. They can then and only then decide whether it should be used.


Structured programs and object-oriented programs can have goto statements and labels in them and still be structured and object-oriented.

Apologies for ranting and raving people ... may be it's the wrong time of the month ... for a man :blink:
Was This Post Helpful? 0
  • +
  • -

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