Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Future Improvements (https://forums.graalonline.com/forums/forumdisplay.php?f=10)
-   -   goto (https://forums.graalonline.com/forums/showthread.php?t=78575)

projectigi 02-02-2008 05:16 PM

goto
 
Hi,

a goto command for gs2 would be nice :o

Chompy 02-02-2008 05:37 PM

Explain? ^^

projectigi 02-02-2008 05:39 PM

well like

PHP Code:

function onCreated()
  {
  if( 
foobar )
    goto 
blub;
  
player.chat "d";
  }

function 
onPlayerChats()
  {
  
Label blub;
  
player.chat "c";
  } 


and it will go to the script part where the label blub is and continue there, so if foobar is true the player will say c @ onCreated
else he will say d, but when foobar is true he wont say d at any time

Chompy 02-02-2008 05:49 PM

Quote:

Originally Posted by projectigi (Post 1373291)
well like

PHP Code:

function onCreated()
  {
  if( 
foobar )
    goto 
blub;
  
player.chat "d";
  }

function 
onPlayerChats()
  {
  
Label blub;
  
player.chat "c";
  } 


and it will go to the script part where the label blub is and continue there, so if foobar is true the player will say c @ onCreated
else he will say d, but when foobar is true he wont say d at any time

well, I don't see any use of it tho :( Unless you give other examples :o

cbk1994 02-02-2008 05:58 PM

I am 100% completely against this idea.

This completely kills the idea of object-orientation.

This is what we have functions for.

You couldn't even justify goto in GS1, let alone GS2.

Skyld 02-02-2008 06:29 PM

There is absolutely no need for this. You have functions and event triggers for a reason.

goto only really works in completely linear languages, of which GScript is not one.

Novo 02-02-2008 07:00 PM

goto's are only useful ( really ) when you want to break out of two loop controls. They are quite obfuscating otherwise. Additionally, there are numerous reports telling us that the more goto's there are in a code, the higher the complexity is.

Plus... There'd be quite a bit of problems with the environment change ( change in function / object / scope ). It's best not to use it at all... In ANY language -- Even ones that support it. The MAJORITY of the cases, they can be completely avoided. In cases that aren't... You could just make a function-call... using return foo();

If you really want to understand... Just imagine Zodiac if goto's existed.

Inverness 02-02-2008 07:28 PM

Absolutely not.

projectigi 02-02-2008 08:08 PM

Quote:

Originally Posted by Novo (Post 1373304)
goto's are only useful ( really ) when you want to break out of two loop controls.

right, its quite annoying to check for a flag each time you have 2+ loops :P
or something like a breakall; that would just kill all the loops instead of the one you are in

basically what you're saying is kinda weird, that sounds like just because c++ has goto's every game only uses them instead of loops

Skyld 02-02-2008 08:42 PM

Quote:

Originally Posted by projectigi (Post 1373329)
right, its quite annoying to check for a flag each time you have 2+ loops :P

How does that have anything to do with wanting "goto"?
Quote:

Originally Posted by projectigi
or something like a breakall; that would just kill all the loops instead of the one you are in

It's called "return", and will end function execution there and then, regardless of how many loops you have.

projectigi 02-02-2008 08:46 PM

Quote:

Originally Posted by Skyld (Post 1373337)
How does that have anything to do with wanting "goto"?

PHP Code:

for( foo 0foo 100foo++ )
  {
  for( 
bar  0bar 100bar++ )
    {
    
mehfoo ][ bar ] = blub;
    if( 
/* do something that would exit the loop */ )
      goto( 
lEndL01 );
    }
  }
Label lEndL01:
//after loop 

Quote:

Originally Posted by Skyld (Post 1373337)
It's called "return", and will end function execution there and then, regardless of how many loops you have.

you don't always want to end the whole function :P

Skyld 02-02-2008 08:48 PM

Quote:

Originally Posted by projectigi (Post 1373338)
PHP Code:

for( foo 0foo 100foo++ )
  {
  for( 
bar  0bar 100bar++ )
    {
    
mehfoo ][ bar ] = blub;
    if( 
/* do something that would exit the loop */ )
      goto( 
lEndL01 );
    }
  }
Label lEndL01:
//after loop 

you don't always want to end the whole function :P

Then structure your code better. :confused:

You could easily have those two loops in a function by themselves, and use "return" to end there, and send back the result of your function execution to the calling function.

zokemon 02-02-2008 08:49 PM

Quote:

Originally Posted by Skyld (Post 1373337)
It's called "return", and will end function execution there and then, regardless of how many loops you have.

Example:

PHP Code:

function onCreated() {
  
foo bar;
  var = 
2;
  
doLoop();
  
doOtherThings();
}

function 
doLoop() {
  for (
010i++) {
    for (
010j++) {
      if (
this.stuff[i][j])
        
doLoopThings();
        return;
      }
    }
  }



projectigi 02-02-2008 09:24 PM

Quote:

Originally Posted by Skyld (Post 1373339)
Then structure your code better. :confused:

You could easily have those two loops in a function by themselves, and use "return" to end there, and send back the result of your function execution to the calling function.

if i put every loop into a own function, reading a code with 1000+ lines will kinda be annoying lol

zokemon 02-02-2008 09:36 PM

Quote:

Originally Posted by projectigi (Post 1373347)
if i put every loop into a own function, reading a code with 1000+ lines will kinda be annoying lol

Usually you can find a way to actually make it look better.

Tolnaftate2004 02-02-2008 10:32 PM

Perhaps break uint; to end 'uint' loops.

Inverness 02-02-2008 11:17 PM

Graal has survived without goto and it will continue to do so despite your wishes. You need to structure your code better rather than ask for goto.

cbk1994 02-03-2008 12:56 AM

goto would be a complete nightmare when trying to read code.

Think about it; in languages such as BASIC they have it, but that is not object oriented, and does not have functions.

Think about this ...

function foo() is called. It goes to CAT which is in function bob(), which goes to DOG which is in function moo() ... then eventually does it go back to the original function foo()? Or does it keep advancing?

And THEN, what happens if you GOTO a function, and then the function ends? Your whole script has stopped.

That's not the way GS2 is supposed to work. After you call one function, it goes back to the function it was in before when it's completed. This is how it should work. goto would completely screw this up.

In Java, goto is actually a reserved variable name (you cannot name a function or variable it). I guess they REALLY don't like it.

If you need to quit a loop, return is your best option, or to use a variable I suppose.

Simply structure your code better is my suggestion.

Googi 02-03-2008 05:22 AM

In PHP you can break out of multiple levels of loops by just going "break 2;" for two levels, "break 3;" for three levels, etc.

Novo 02-03-2008 07:13 AM

Based on a Case-Study done in Code Complete 2ed, by MSPress... This is what they concluded about the use of goto's:

( pg. 408 )

Quote:

The Argument Against gotos

The general argument against gotos is that code without gotos is higher-quality code.

The famous letter that sparked the original controversy was Edsger Dijkstra's "Go To
Statement Considered Harmful" in the March 1968 Communications of the ACM.
Dijkstra observed that the quality of code was inversely proportional to the number of
gotos the programmer used. In subsequent work, Dijkstra has argued that code that
doesn't contain gotos can more easily be proven correct.

Code containing gotos is hard to format. Indentation should be used to show logical
structure, and gotos have an effect on logical structure. Using indentation to show the
logical structure of a goto and its target, however, is difficult or impossible.

Use of gotos defeats compiler optimizations. Some optimizations depend on a program's
flow of control residing within a few statements. An unconditional goto makes the flow
harder to analyze and reduces the ability of the compiler to optimize the code. Thus,
even if introducing a goto produces an efficiency at the source-language level, it may
well reduce overall efficiency by thwarting compiler optimizations.

Proponents of gotos sometimes argue that they make code faster or smaller. But code
containing gotos is rarely the fastest or smallest possible. Donald Knuth's marvelous,
classic article "Structured Programming with go to Statements" gives several examples
of cases in which using gotos makes for slower and larger code (Knuth 1974).

In practice, the use of gotos leads to the violation of the principle that code should flow
strictly from top to bottom. Even if gotos aren't confusing when used carefully, once
gotos are introduced, they spread through the code like termites through a rotting house.
If any gotos are allowed, the bad creep in with the good, so it's better not to allow any of
them.

Overall, experience in the two decades that followed the publication of Dijkstra's letter
showed the folly of producing goto-laden code. In a survey of the literature, Ben
Shneiderman concluded that the evidence supports Dijkstra's view that we're better off
without the goto (1980), and many modern languages, including Java, don't even have
gotos.

The Argument for gotos

The argument for the goto is characterized by an advocacy of its careful use in specific
circumstances rather than its indiscriminate use. Most arguments against gotos speak
against indiscriminate use. The goto controversy erupted when Fortran was the most
popular language. Fortran had no presentable loop structures, and in the absence of
good advice on programming loops with gotos, programmers wrote a lot of spaghetti
code. Such code was undoubtedly correlated with the production of low-quality
programs, but it has little to do with the careful use of a goto to make up for a gap in a
modern language's capabilities.

A well-placed goto can eliminate the need for duplicate code. Duplicate code leads to
problems if the two sets of code are modified differently. Duplicate code increases the
size of source and executable files. The bad effects of the goto are outweighed in such a
case by the risks of duplicate code.

The goto is useful in a routine that allocates resources, performs operations on those
resources, and then deallocates the resources. With a goto, you can clean up in one
section of code. The goto reduces the likelihood of your forgetting to deallocate the
resources in each place you detect an error.

In some cases, the goto can result in faster and smaller code. Knuth's 1974 article cited a
few cases in which the goto produced a legitimate gain.

Good programming doesn't mean eliminating gotos. Methodical decomposition,
refinement, and selection of control structures automatically lead to goto-free programs
in most cases. Achieving goto-less code is not the aim but the outcome, and putting the
focus on avoiding gotos isn't helpful.

Decades' worth of research with gotos failed to demonstrate their harmfulness. In a
survey of the literature, B. A. Sheil concluded that unrealistic test conditions, poor data
analysis, and inconclusive results failed to support the claim of Shneiderman and others
that the number of bugs in code was proportional to the number of gotos (1981). Sheil
didn't go so far as to conclude that using gotos is a good idea—rather, that experimental
evidence against them was not conclusive.

The evidence suggests only that deliberately chaotic control structure degrades
[programmer] performance. These experiments provide virtually no evidence for the
beneficial effect of any specific method of structuring control flow.
—B. A. Sheil

Finally, the goto has been incorporated into many modern languages, including Visual
Basic, C++, and the Ada language, the most carefully engineered programming
language in history. Ada was developed long after the arguments on both sides of the
goto debate had been fully developed, and after considering all sides of the issue, Ada's
engineers decided to include the goto.
I hope that gives a premise of debate on the topic.

If you wish to read more on the topic, you could read it, if you register for free, using the http://cc2e.com/ website.

projectigi 02-03-2008 03:49 PM

Quote:

Originally Posted by cbkbud (Post 1373399)
goto would be a complete nightmare when trying to read code.

Think about it; in languages such as BASIC they have it, but that is not object oriented, and does not have functions.

Think about this ...

function foo() is called. It goes to CAT which is in function bob(), which goes to DOG which is in function moo() ... then eventually does it go back to the original function foo()? Or does it keep advancing?

And THEN, what happens if you GOTO a function, and then the function ends? Your whole script has stopped.

That's not the way GS2 is supposed to work. After you call one function, it goes back to the function it was in before when it's completed. This is how it should work. goto would completely screw this up.

In Java, goto is actually a reserved variable name (you cannot name a function or variable it). I guess they REALLY don't like it.

If you need to quit a loop, return is your best option, or to use a variable I suppose.

Simply structure your code better is my suggestion.

C++ is OOP and they have it :P

cbk1994 02-03-2008 03:53 PM

Basically all the arguments FOR goto can be done with functions, and the rest of them don't really apply to Graal.

Gotos are largely used in languages with no functions or logical structure.

EDIT:
Yes, C++ has it, but if it was added into Graal it would just be a mess. I'm not saying it would be bad for every object oriented programming language, sorry if it came out that way, but it would mean searching through code to find that label, or however you do it, and then executing from there. The if the function ends, the code stops, because he was saying that he wants it to break from loops. Here is an example:

PHP Code:

function onCreated()
{
  for ( 
temp.foo )
  {
    if ( 
temp.== "bar" )
    {
      goto 
crap;
    }
  }
  
player.chat "gah"// won't happen
}
function 
myHouse()
{
  
label crap// however this is done
  
player.chat "yey im out";
// osht, the code is done running. 


Codein 02-03-2008 09:35 PM

Quote:

Originally Posted by projectigi (Post 1373452)
C++ is OOP and they have it :P

Much like saying cocaine abuse is around us. Doesn't necessarily mean we should use it.

I generally prefer to steer clear from gotos. Functions are much better to use.

cbk1994 02-03-2008 10:49 PM

Quote:

Originally Posted by Codein (Post 1373508)
Much like saying cocaine abuse is around us. Doesn't necessarily mean we should use it.

It doesn't? That guy in the white Sedan was lying to me!

Admins 02-04-2008 12:30 AM

C++ supports a very limited version of "goto", of course you cannot jump into other functions or even outside of the current scope (when you have declared variables). The compiler is basicly breaking the code down to several if-else statements. That is exactly what you should do too, it will make the code look more clean.

In some other languages you can break a specific loop by adding a label in front of the loop and then saying "break labelname". It might be easier to stay with stuff that everyone knows though, makes code more readable instead of introducing a lot of GS2-specific constructs.


All times are GMT +2. The time now is 11:23 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.