Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Gosub is obsolete?

Author
Message
Richard_6
7
Years of Service
User Offline
Joined: 3rd Feb 2017
Location:
Posted: 7th Feb 2017 13:25
Documentation states that Gosub is not recommended to be used. However, it seems very useful for structuring whats going to be inside the do... sync.. loop.

What is your opinion about this? Have you been using or avoiding?
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 7th Feb 2017 13:56 Edited at: 7th Feb 2017 13:57
No!.

That's my opinion. Personally, I think that using gosub is bad practice as it could lead to messy, complex spaghetti code.
I am of the opinion that if you feel something should be made into a subroutine, then it would be better as a reusable function.

Some people believe it makes some things easier, but I've never used a gosub in any AppGameKit project. Gosub is a hold over to the old way of programming, (before variable scoped functions were created) whereas we should be moving to the new way. (Or as close as we can with what advanced features AppGameKit gives us)
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 7th Feb 2017 13:58
myself using Functions, you will have a better overview and control, also better for debugging.
sometimes i also using labels for goto, but very rarely.
AGK (Steam) V2017.01.09 : Windows 10 Pro 64 Bit : AMD (16.12.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 7th Feb 2017 14:22
It's personal preference.
But you will find that people here who program for a living (myself included) tend to use functions rather than gosub.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 7th Feb 2017 14:42 Edited at: 7th Feb 2017 15:03
The use case for gosub instead of a function is pretty narrow and not very common:

The biggest difference is functions each contain their own local scope, any non global variables declared inside a function only exist within tbe function while the function is called they are then disposed. You can pass outside variables into the function when calling it, and you can return a variable or value from the function when it ends.

On the otherhand subroutines exist in the root scope and can access any other non global variable in the root scope even if they are not declared in the sub.

Given this, what is the use case for a sub? when you need to access many non global variables in the root scope and dont want to have to pass them all into a function as arguments.

This is very uncommon however, as anytime you find yourself working with that many root scope variables, you would be better off changing your approach and reorganizing your code into user defined types which group complex data of many variables into a fewer number of top level variables that can more easily be passed around and managed.

The other use case is when you need to make persistent changes to many root scope variables and don't want to make them all global. this is a bit more compelling an argument for using a sub, but again you can usually change your approach into a more modular design which eliminates the need to change so many root scope variables in a single function/routine.

Much like globals, subroutines do have their uses, and it can't reasonably be said that you should never under any circumstance use them, but you should avoid them unless you have a firm understanding of when to use them and why.
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 7th Feb 2017 15:31
Quote: "sometimes i also using labels for goto,"

I just vomited in my mouth a little.
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 7th Feb 2017 15:48
Me too.
I used to respect Markus as a coder. I don't know what to think anymore
AGK V2 user - Tier 1 (mostly)
Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 7th Feb 2017 15:59 Edited at: 7th Feb 2017 16:01
Outside of a .bat file, I really can't say I have ever encountered a case in which goto was a good option.

If you are using it to skip processing a section, an if conditional is better, if you are using it to jump back and reprocessing something, reprocess it on the next loop pass, or maybe use a recursive function (carefully) if it really had to be reprocessed immediately.

I can't think of any other reason to use goto, but I'd be interested to see a good case for it...
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 7th Feb 2017 16:10
Quote: "but I'd be interested to see a good case for it..."

There isn't one. It's a crutch for a bad programmer to hide their limp.
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 7th Feb 2017 16:15
As much as I hate GOTO and have never used it, I have thought of one possible good case for using it:


Now I need a shower. I feel dirty!
AGK V2 user - Tier 1 (mostly)
Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 7th Feb 2017 16:29
Why not



?
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 7th Feb 2017 16:45
No reason at all.
I was just playing Devils advocate and defending the indefensible. And I hate myself a little bit for it.

AGK V2 user - Tier 1 (mostly)
Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 7th Feb 2017 16:48
I use it quite often, mostly with initiate routines that are only ever called once. I mean, you can't declare a type inside a function, and you don't want all that stuff cluttering up the start of your code, so I tend to make a subroutine for setting up variables, globals, arrays etc.

I prefer not to declare globals or arrays inside a function, to me, a function is something that you call several times, a subroutine is something you use to keep code organized... I know some people hate it, but part of the reason why I like using AppGameKit is that you can do things your own way, and over a dozen completed projects later, I'm unlikely to change now - I say save the strict protocols for when you have to use Visual Studio or XCode.
The code is dark and full of errors
Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 7th Feb 2017 17:01
That's when I break things into modules, put all the declarations at the top of the module, provide an init function that will set (and reset) initial values, then use include and init in the main app before entering the main loop.

I did used to wrap both the module declarations and init into a module init subroutine for many years, there was nothing wrong with it, just preference.
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 7th Feb 2017 17:39
Quote: "That's when I break things into modules, put all the declarations at the top of the module, provide an init function that will set (and reset) initial values, then use include and init in the main app before entering the main loop."


That's exactly my method
Although you can also put the declarations at the bottom out of the way, and the compiler still finds them.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
SpecTre
Developer
21
Years of Service
User Offline
Joined: 24th Feb 2003
Location: UK
Posted: 7th Feb 2017 18:35
I didn't mind using Gosub about 6 months ago but tend not to use it now unless, as you say, when breaking up into modules.
Mainly stopped using Gosub when started Tier 2 coding and with OOP, would be good to see OOP in AppGameKit Tier 1
The Amiga and Amos were great!
MadBit
VIP Member
Gold Codemaster
15
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 7th Feb 2017 20:18
The last time I had used goto and gosub was in amigabasic . I also never missed it.
Share your knowledge. It\'s a way to achieve immortality. (Tenzin Gyatso)
Pixie-Particle-Engine
easter bunny
11
Years of Service
User Offline
Joined: 20th Nov 2012
Playing: Dota 2
Posted: 7th Feb 2017 22:39
Quote: "if [user selectes quit] then endprogram()
...
...
...
Function endprogram()
DeleteAllSprites()
DeleteAllImages()
DeleteAllObjects()
DeleteAllText()
end
Endfunction"

I cannot stand the sight of ending a program from inside a function...

I always structure my programs something like this:



VanB wrote: "I use it quite often, mostly with initiate routines that are only ever called once. I mean, you can't declare a type inside a function, and you don't want all that stuff cluttering up the start of your code, so I tend to make a subroutine for setting up variables, globals, arrays etc."

This is what I do. Basically for code that I would place in the main body, but dont want tohave cluttering up main.agc

Quote: "would be good to see OOP in AppGameKit Tier 1"

Well classes was on of the things we could vote for in the questionaire a few weeks back. Hopefully it makes it in

My Games - Latest WIP
130,000 installs with AppGameKit and counting
CumQuaT
AGK Master
14
Years of Service
User Offline
Joined: 28th Apr 2010
Location: Tasmania, Australia
Posted: 7th Feb 2017 23:09
It's funny, actually. I used (and still use) GOSUB a LOT in DBpro, but literally never when coding in AGK2. I've simply never needed it.
I work full-time making games in AGK2 and DBpro. Living the dream!
Stab in the Dark software
Valued Member
21
Years of Service
User Offline
Joined: 12th Dec 2002
Playing: Badges, I don't need no stinkin badges
Posted: 7th Feb 2017 23:33
I stopped using GOSUB in 1985. When ever I see a GOSUB I feel like Taylor when he sees the statue of liberty at
the end of the planet of the apes movie. LOL

On that note I think we scared off the OP.
The coffee is lovely dark and deep,and I have code to write before I sleep.
CodeName
7
Years of Service
User Offline
Joined: 30th Dec 2016
Location:
Posted: 8th Feb 2017 01:18 Edited at: 8th Feb 2017 07:24

Do

JUMP="_”+Str(GetSocketConnected(SocketID))
Gosub JUMP

Sync()
Loop

_-1:
Print( “ Failed to Connect ” )
Return

_0:
Print( “ Trying to Connect ” )
Return

_1:
Print( “ TCP EnGaGeD Mu hu haha “ )
Return
Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 8th Feb 2017 02:59 Edited at: 8th Feb 2017 13:46
Quote: "
Do

JUMP="_”+Str(GetSocketConnected(SocketID))
Gosub JUMP

Sync()
Loop

_-1:
Print( “ Failed to Connect ” )
Return

_0:
Print( “ Trying to Connect ” )
Return

_1:
Print( “ TCP EnGaGeD Mu hu haha “ )
Return"


Clever, but unless you are dealing with a great number of cases I can't imagine you will see a significant speed difference vs select:case, and you introduce bouncing in the flow control which can affect readability
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
CodeName
7
Years of Service
User Offline
Joined: 30th Dec 2016
Location:
Posted: 8th Feb 2017 04:59 Edited at: 8th Feb 2017 07:24
Linus, would beg the differ. haha
CodeName
7
Years of Service
User Offline
Joined: 30th Dec 2016
Location:
Posted: 8th Feb 2017 05:22 Edited at: 8th Feb 2017 06:40
Explaining my code example..

http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html

"The C and C++ standards do not support this feature. However, the GNU Compiler Collection (GCC)
includes a non-standard extension for doing this as described in this article. Essentially, they have
added a special operator "&&" that reports the address of the label as type "void*". See the article for details.
P.S. In other words, just use "&&" instead of "&" in your example, and it will work on GCC.
P.P.S. I know you don't want me to say it, but I'll say it anyway,... DON'T DO THAT!!!"
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 8th Feb 2017 09:11 Edited at: 8th Feb 2017 09:12
@CodeName: Why not...
CodeName
7
Years of Service
User Offline
Joined: 30th Dec 2016
Location:
Posted: 8th Feb 2017 09:50 Edited at: 8th Feb 2017 09:53
Yep.. I don't like using If Statements.. Or Nor Not =to ect..

Select Statements are my favorite, I myself never use Goto or Gosub, not it being a rule, just no use for them.

When I get this Socket coding done, I'll post a complete example showing how I use Select Statements for Networking...

:Note the code here was in my help post on steam, I'm not showing the true use of this Select Statement.

Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 8th Feb 2017 18:20
Quote: "sometimes i also using labels for goto, but very rarely"


for any reason, here i used it, maybe someone have a better solution? (agk v1>v2)

AGK (Steam) V2017.01.09 : Windows 10 Pro 64 Bit : AMD (16.12.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 9th Feb 2017 09:45
No offense Markus, bu that code with GOTOs within a function make me ill.

I can't think of a worse way to do it!
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 9th Feb 2017 10:48
I love GOSUB and GOTO!

.... just for the nostalgia trip of course...

I haven't used either since I was about 5 on my C64:
10 PRINT "HELLO WORLD ";
20 GOTO 10

awesome.
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 9th Feb 2017 11:19
Quote: "10 PRINT "HELLO WORLD ";
20 GOTO 10"

Ahh, every proper coder's first program. Sweet memories, I first typed this on an old BBC B microcomputer. (Before I even got my first c64!)
Dybing
13
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 9th Feb 2017 14:22 Edited at: 9th Feb 2017 14:23
Amazingly, even in C/C++ goto is still legal. I'm not a fan, and I see no reason to keep goto/gosub around in AGK. In C/C++ I see why they keep it around due to some very ancient and very important legacy code at some very big organizations that they can't risk braking. In AppGameKit? Not so much. Just nip it out, put a band-aid on the fresh wound and distract the patient with a lollipop or something.



CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 9th Feb 2017 14:49
No. Keep GOTO and GOSUB. Ain't nothing wrong with spaghetti. It's often advantageous to obfuscate code, and the ability to jump around at will could certainly help.
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 9th Feb 2017 15:42
Mobiius
you not wrote a better solution.
i used it there because i not want leave the task
config dialog, and because we not have windows
i clear current form, open a sub form, and after return then
i show up the dialog ui again.

AGK (Steam) V2017.01.09 : Windows 10 Pro 64 Bit : AMD (16.12.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 9th Feb 2017 16:35
State machines.

That is all.

Goto = EVIL!!!
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 9th Feb 2017 17:05
Quote: "State machines"

making things over complicated = evil
its my code, i can be much evil as i want.
he he
in some cases i used the best compromise.
the sub dialogs i built in later and i not need
rewrote the main dialog.
AGK (Steam) V2017.01.09 : Windows 10 Pro 64 Bit : AMD (16.12.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 9th Feb 2017 22:15 Edited at: 9th Feb 2017 22:16
On some rare occasions, when it comes to speed, especially on massive calculations, and subcode is executed hundreds of thousands of times, gosub and goto are faster than functions (also depends on the compiler).
There are also some other exceptions, like: (wiki)
Quote: "These uses are relatively common in C, but much less common in C++ or other languages with higher-level features.[35] However, throwing and catching an exception inside a function can be extraordinarily inefficient in some languages; a prime example is Objective-C, where a goto is a much faster alternative.[37]

Another use of goto statements is to modify poorly factored legacy code, where avoiding a goto would require extensive refactoring or code duplication. For example, given a large function where only certain code is of interest, a goto statement allows one to jump to or from only the relevant code, without otherwise modifying the function. This usage is considered code smell, but finds occasional use."


Let's face it guys, without jumps, there is no assembler and machine code, so without goto we would be ******

I don't use goto and gosub myself, but if people want to use them, let them.
There is no perfect code style. The best code style is the one that works out for you

Of course, goto is a bitch, and thou shall not use it

10 Poke 1,1
20 Goto 10

https://xkcd.com/292/


Cheers,
PSY

Goto BED
PSY LABS Games
Coders don't die, they just gosub without return
MadBit
VIP Member
Gold Codemaster
15
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 10th Feb 2017 09:12 Edited at: 10th Feb 2017 09:15
@Markus
Markus wrote: "Mobiius
you not wrote a better solution.
i used it there because i not want leave the task
config dialog, and because we not have windows
i clear current form, open a sub form, and after return then
i show up the dialog ui again.
"


How is this
3 lines commented out. (lines: 11, 258, 266)
2 lines added. (lines: 12, 268)

Not tested, but should work.


Share your knowledge. It\'s a way to achieve immortality. (Tenzin Gyatso)
Pixie-Particle-Engine
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 10th Feb 2017 13:40
Quote: "Documentation states that Gosub is not recommended to be used. However, it seems very useful for structuring whats going to be inside the do... sync.. loop.

What is your opinion about this? Have you been using or avoiding? "


Avoiding at all costs.
It is considered a bad practice for structured programming and for maintenance purposes.

Also, using them is usually prohibited if the code is going to be under any quality auditing.

I never ever use them and I hate to read a code that uses them.
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 10th Feb 2017 16:34
MadBit
jo, it looks different.
AGK (Steam) V2017.01.09 : Windows 10 Pro 64 Bit : AMD (16.12.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
damothegreat
User Banned
Posted: 12th Feb 2017 19:55 Edited at: 12th Feb 2017 19:56
all my projects are functions.

try not use gosubs and definitely not goto's... becomes messy and not structured programming what so ever.

messy code = bad performance.
structured code = best performance.

it would be like putting the washer on with 10 king size duvets in it, it will say STOP, your giving me too much work to do and then fail miserably

Login to post a reply

Server time is: 2024-09-29 23:33:11
Your offset time is: 2024-09-29 23:33:11