Too many Goto Statements

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Too many Goto Statements

#1 Post by PaperTronics » 02 Aug 2017 06:16

I've heard at many batch programming forums and from many batch programmers that using many goto statements in your code isn't good and that the programmer will loose the track of his code.

I'm creating a text-based game and it contains 34 goto statements and the game isn't even 25% complete!

The number of goto statements will be over a 200 if I keep using them.


So is there an alternate command or trick that will help me keep track of my code?


Any help is greatly appreciated!
PaperTronics

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Too many Goto Statements

#2 Post by aGerman » 02 Aug 2017 10:05

PaperTronics wrote:I've heard at many batch programming forums and from many batch programmers that using many goto statements in your code isn't good and that the programmer will loose the track of his code.

viewtopic.php?t=7693
Most languages don't even have something like GOTO. The reasons why you may need GOTO in Batch can be found in the above link.

PaperTronics wrote:So is there an alternate command or trick that will help me keep track of my code?

As like in other languages you can call procedures (subroutines, functions). After you left the procedure the code will always continue at the point from where it was called. This makes it much easier to track. Furthermore you can pass arguments. This might be helpful to avoid redundant code.

GOTO

Code: Select all

@echo off &setlocal
choice /c lr /m "Where do you want to go to"
if errorlevel 2 (goto right) else goto left

:right
echo You went right and came to a big old oak tree.
pause
goto :eof

:left
echo You went left and came to a bridge over the river.
pause
goto :eof



CALL (with the same behavior)

Code: Select all

@echo off &setlocal
choice /c lr /m "Where do you want to go to"
if errorlevel 2 (call :output "right" "big old oak tree") else call :output "left" "bridge over the river"
pause
exit /b

:output
echo You went %~1 and came to a %~2.
exit /b

Steffen

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: Too many Goto Statements

#3 Post by PaperTronics » 03 Aug 2017 04:10

@aGerman - I don't think that using call will be the accurate solution. Because in my game, I have the main choices, which lead to the different storyline in which there are sub choices and those sub choices lead to a different storyline and the process continues.


So using call will be the same as using goto



PaperTronics

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Too many Goto Statements

#4 Post by aGerman » 03 Aug 2017 10:51

Just have a look at Dave's adventure game.
viewtopic.php?f=3&t=4876
I didn't try to understand the code because I'm not interested in games. (Also batch was mainly made for repeatedly executed stupid automation-scripts. Definitely not for developing games.)
I saw that he used a lot of GOTOs but also a lot of value pairs. Maybe some of the text will be just picked up by calculating an index.

Steffen

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: Too many Goto Statements

#5 Post by PaperTronics » 04 Aug 2017 05:41

I decoded Dave's game and saw that he used many Goto's (171 to be exact), but in a clever way. I'm gonna try and do the same thing and see if it works for me



PaperTronics

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Too many Goto Statements

#6 Post by Aacini » 04 Aug 2017 08:25

Here there are my own thoughts about this matter.

It is better to write programs with no GOTO statements/commands whichever is the programming language used. The reasons are multiple, but a very short way to summarize they is this explanation:

Sooner or later a program will be modified. If the program is clear and understandable, the modification is simple. If the program is obscure, the modification may cost even more in terms of time and money than rewritting the whole program! The use of GOTO's is one of the sole points that increases program complexity and make code hard to read. The other important point that can make a program clearer is the documentation.

About mid-60's these factors (combined with others more) impacted the computer industry in the so-called Software crisis as "... the software industry's inability to provide customers with high quality products on schedule. In general it refers to poorly written, hard to read, error-prone software that often lacks good documentation". The way to solve such a crisis was the development of a series of "structured" techniques to write program code. The "Structured programming languages" like Pascal, the first one of them, and all its succesors have one point in common: force or convince the user to not use GOTO...

In the case of Batch files there are a couple of details: a loop assembled via GOTO is much slower than a loop written with a FOR command, but there are some constructs that may be written in a simpler/shorter way using GOTO that other methods. In general, a Batch file may be written with a minimum of GOTO's. If a program requires a lot of them (in any language), then it could have a poor design. Finally, I don't think that Dave's game be a good example of GOTO use in Batch files because his program was a translation of Fortran code.

Antonio

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: Too many Goto Statements

#7 Post by PaperTronics » 05 Aug 2017 20:22

@Aacini - So what do you suggest that I should do? I am gonna provide a clear documentation, but what should I do about the GOTOs. I can't remove them :cry:

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Too many Goto Statements

#8 Post by ShadowThief » 05 Aug 2017 20:34

Leave the gotos in, imo. Without seeing the code, it's hard to say how it can be improved. If you've got menus inside of menus inside of menus, a lot of them can probably be replaced with calls, and I've seen code where there are 20+ if statements that all end in gotos inside the same subroutine that all basically do the same thing but with different parameters and that can be drastically simplified, but if you're at a label and you're going somewhere and don't plan on coming back, use a goto. If you need to break out of a for loop before it would normally terminate, use a goto. If you need an infinite loop for some reason, use a goto.

It's better to finish writing the code first and then try to optimize it later.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Too many Goto Statements

#9 Post by Aacini » 05 Aug 2017 21:25

PaperTronics wrote:@Aacini - So what do you suggest that I should do? I am gonna provide a clear documentation, but what should I do about the GOTOs. I can't remove them :cry:


I suggest you to post your code, so we all know what are you talking about and we may give suggestions to modify it...

If you have 20 topmost sections in your main menu, just post 4; if inside each one of the topmost sections there are 8 sub-sections, just post 3, etc... This will give us a good look at your code in less than 30 lines...

Antonio

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: Too many Goto Statements

#10 Post by PaperTronics » 07 Aug 2017 07:06

ShadowThief wrote:but if you're at a label and you're going somewhere and don't plan on coming back, use a goto


That's exactly the situation that is happening with me.

I don't have any type of menus in my game, but I lot of choices that will result in a different storyline.

My code is too long to be easily read so I'm just gonna post a section of my code:

Code: Select all

:PLaneHijack 
cls
set chkpoint=PlaneHijack
>!sys!/!savedata! Echo !chkpoint!-!codename!

Fn.dll sprite 5 0 F9 "Oh No^! John Black's hijackers have hijacked your flight^! They don't know that"
Fn.dll sprite 6 0 F9 "you are a Detective, but they will soon if you don't stop them, as they are"
Fn.dll sprite 7 0 F9 "stealing everyone's stuff and if they go through yours, it will be an extremely"
Fn.dll sprite 8 0 F9 "astonishing sight for them^! You will have to stop them quickly^!"
Call Box 49 14 7 30 - - - f9 -
Fn.dll sprite 15 50 F9 "Some choices have specific"
Fn.dll sprite 16 50 F9 "timers and if you don't"
Fn.dll sprite 17 50 F9 "select a choice within"
Fn.dll sprite 18 50 F9 "that time, it's game"
Fn.dll sprite 19 50 F9 "over for you^!"

Fn.dll sprite 21 55 F9 "Press any key"
pause >nul
Fn.dll sprite 15 0 F9 "1 - Kill the first hijacker secretly with a knife"
Fn.dll sprite 16 0 F9 "2 - Try to kill both the hijackers with your gun"
Call :TimedChoice 12 6 3 HijackChcChk StuffStolen

:HijackChcChk
if "%ErrorLevel%"=="1" (Goto :SecretKnife)
if "%ErrorLevel%"=="2" (Goto :GunKill)



Now, both of the choices, GunKill or SecretKnife will lead to another story which will have different choices and so on..


So, is there a way to optimize my code and minimize the number of GOTO statements or will I have to continue using them?




P.S: I know I use a lot of external plugins :mrgreen:
PaperTronics

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Too many Goto Statements

#11 Post by Aacini » 07 Aug 2017 08:05

This is the way I would do it:

Code: Select all

@echo off

:Level0
:Level1-1-3
cls
echo Top level menu:
echo 1- Big
echo 2- Middle
echo 3- Small
echo 0- Exit
choice /C 1230 /N /M "Select size: "
goto :Level1-%errorlevel%

   :Level1-1 Big
   echo You selected Big
   echo 1- Up
   echo 2- Down
   echo 0- Go back
   choice /C 120 /N /M "Select vertical: "
   goto :Level1-1-%errorlevel%

      :Level1-1-1 Up
      echo You selected Up
      pause
      goto :Level1-1-3

      :Level1-1-2 Down
      echo You selected Down
      pause
      goto :Level1-1-3

   :Level1-2 Middle
   echo You selected Middle
   pause
   goto :Level0

   :Level1-3 Small
   echo You selected Small
   pause
   goto :Level0

:Level1-4
echo You selected Exit...

IMHO you don't need to write all game options inside the Batch file. You may extract the options into a text data file and then just write a Batch program to read such a file and select/execute the options accordingly. See this topic for an example of such a method.

Antonio

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: Too many Goto Statements

#12 Post by PaperTronics » 08 Aug 2017 11:53

Thanks for the help Aacini, I took some time and thought about your answer and decided that I should follow it since no other method seemed to spark up in my mind.

I've decided to create a plugin similar to that one that you linked, but as an addition, it also allows you to select from 20 different menu frameworks and much much more options too!

But the other options are for the future, till now, I'm just creating a simple Batch file to read n' write the options as in the text file specified.




Thanks again,
PaperTronics

Post Reply