ID:152560
 
I've been reading some old forum posts about goto being evil and ineffecient. I'm curious, if it is 'evil' why is it in the DM language? Or does it actually have a useful function?(If so could someone show me an example?)
It isn't so much evil as normally misused, like usr in procs - it could be used, but the result is potentially unpredictable and you may not be able to debug the code easily.
Using goto also encourages programming in hugemongous sections of code where smaller procedures should be used and called instead. This makes it easier to pinpoint where the bit of code you want to change is as you can skip directly to procedure in the tree view of DM but not skip to label without using find.
Goto and similar keywords are used in many programming languages. I believe this may have been an offshoot from the ability to use labels in assembly code, and simply been mimic'd by third generation languages.
As Hazman said, it is not so much evil and without use as it is used wrong by most people.

About it being inefficient though, unless Byond handles goto in a strange way goto is not at all inefficient. That is assuming you mean inefficient for the computer's processor. If you mean inefficient from an organization of code perspective, that may well be.
TheLunarWolf wrote:
I've been reading some old forum posts about goto being evil and ineffecient.

I wouldn't call it "evil," certainly. As for "ineffecient," well, it isn't ineffecient at all from the perspective of the computer, but from the perspective of the programmer, on the other hand, it makes code harder to debug. Why? Because every time you write "goto [tag]," you're going to then have to find the place where you wrote "[tag]" to know what's going on. This may not seem so bad when you originally write your code because you remember where everything is at that point, but when you take a two-month-long break from your program and then return to it, you'll have a simply awful time figuring out exactly what's going on.

I'm curious, if it is 'evil' why is it in the DM language? Or does it actually have a useful function?(If so could someone show me an example?)

Goto sometimes becomes useful when you get into loops within loops; however, I've personally never come across in my own programming the need to use goto. It'd have to be a pretty complex situation, and even then, with a little thought, you could probably come up with a better-organized solution.

That said, I remember Lummox having found an occasion to use it in his Regex library (hub://lummoxjr.regex), but to access that, you would have to pay the library's subscription fees.
In higher-level languages, goto is the control statement of last resort. It's good to have in those cases, but not necessary for most situations.

My Regex library is one place where goto has come in handy, because the loops involved are quite complex. Another place goto is handy is in a piece of sudoku solving code, which is mighty difficult to change to a non-goto format as I discovered when porting it to Java.

Java has no goto statement, even though its internal bytecode does, because effete lunkheads had a great deal of say in designing the language. They too came from the school of thought that goto is evil. As any good programmer knows, it's merely something you want to avoid at great cost, but sometimes it makes all the difference. Dan and Tom were blessed with the wisdom to recognize that distinction.

Lummox JR
to is inefficient too.

It's much slower than a normal for() loop.
In response to Wizkidd0123
Wizkidd0123 wrote:
Because every time you write "goto [tag]," you're going to then have to find the place where you wrote "[tag]" to know what's going on.

That's what they have the search function for. So you can easily find stuff like that. If you ever need to go back through something, I like to try and give it a read-through first to try and get into the same mindset I was in when I wrote it.

I actually use goto in one of my battle systems for a game I am making. I dunno if it is a proper use of it or not, but if anyone wants to see it, feel free to ask...

§atans§pawn
In response to Satans Spawn
Satans Spawn wrote:
I actually use goto in one of my battle systems for a game I am making. I dunno if it is a proper use of it or not, but if anyone wants to see it, feel free to ask...

If you don't know, then it isn't. It's that simple. Use while() or for() instead.

Lummox JR
In response to Lummox JR
I've had one place where I found I needed goto, which is in my character creation, where it goes to another spot when a player makes a mistake in creating the character. Right now I'm trying to find a better way of doing this, because I don't like it as it is right now.
In response to CaptFalcon33035
CaptFalcon33035 wrote:
to is inefficient too.

It's much slower than a normal for() loop.

Uhm, I don't think so.
#define DEBUG
mob/verb
One()
var/i=1
while(i<5000)
i++
world<<i
Two()
var/i=1
Label:
i++
world<<i
if(i<5000)
goto Label:
Three()
for(var/i=1,i<=5000,i++)
world<<i


My "profile world" showed goto took only 0.031 seconds longer than for().
In response to Airjoe
Oh my, we must now all avoid it like the plague. You know impatient people are when they have to wait for 3 hundredths of a second.

I don't think he was talking about that though. He said "to is ineficient too," which I take to mean the style of for(var/index = 1 to 100)
In response to Popisfizzy
Use while()

var/possname
while(!src.name)
possname=input("Enter Name","Character Creation",src.key) as text
if(CheckName(possname))
src.name = possname


I think that is what you are talking about atleast.
In response to Popisfizzy
Popisfizzy wrote:
I've had one place where I found I needed goto, which is in my character creation, where it goes to another spot when a player makes a mistake in creating the character. Right now I'm trying to find a better way of doing this, because I don't like it as it is right now.

You did not need goto; you merely thought you did. There's a difference. The latter means you don't know how to use while() and for() loops properly.

Lummox JR
In response to Satans Spawn
Chances are, it's not a proper use. As someone else somewhere in this thread said, it's only proper in heavily nested loops, sometimes. The only other place I've seen it is C error handling, and that's because C lacks any other ways to handle errors.
In response to Loduwijk
Loduwijk wrote:
I don't think he was talking about that though. He said "to is ineficient too," which I take to mean the style of for(var/index = 1 to 100)

Ah, that would make more sense. I misunderstood.
In response to Airjoe
so basically what you're saying(lummoxJR) is that unless you know how it works and there isn't another way of doing what you're looking for is to avoid using it?(I've never actually needed to use it as my proc loops aren't that complex)

PS Does JR stand for Just Right?
In response to Audeuro
Audeuro wrote:
Chances are, it's not a proper use. As someone else somewhere in this thread said, it's only proper in heavily nested loops, sometimes. The only other place I've seen it is C error handling, and that's because C lacks any other ways to handle errors.

Error handling is indeed another fairly decent use of goto, sometimes even in BYOND. Where try-catch isn't available, it's the next best thing.

Also this comes up in loops that are not necessarily heavily nested, but are too complex for simple while() to work with. The sudoku problem I mentioned is one; its loop is basically composed of three sections, any one of which may be skipped each time. Using extra if() statements to achieve the same effect would turn this tight loop--which it has to be for speed--into a monster.

Lummox JR
In response to Airjoe
I was talking about the "to" operator. Y'know, I think it's undocumented. But, yeah, your information was useful. How long did the while() take?
In response to CaptFalcon33035
CaptFalcon33035 wrote:
I was talking about the "to" operator. Y'know, I think it's undocumented.

Eek, yeah, I misunderstood.

But, yeah, your information was useful. How long did the while() take?

IIRC, it was just between the goto and the for. You can run these tests yourself by making test procs like the code I showed above, putting "#define DEBUG" in your code, running the game, and using "Profile World" in the "Options Menu".

In response to Audeuro
I'm curious. Would the code below (which I just created but use a similar version of in other code) be considered a good way of using goto?

pword
var
name="secret area" //label
password="secret" //password
list/allowed=list("androidlore") //keys allowed to see the password

mob/verb/pwords()
var/list/pwords[0]
pwords+=new/pword

for(var/pword/X in pwords)
if(ckey=="androiddata")goto allowed
if((ckey in allowed))goto allowed
return
allowed:
src<<X.password
Page: 1 2