DosTips.com

A Forum all about DOS Batch
It is currently 27 Mar 2017 20:22

All times are UTC-06:00




Post new topic  Reply to topic  [ 10 posts ] 
Author Message
PostPosted: 02 Feb 2017 09:42 
Offline

Joined: 23 Nov 2015 09:34
Posts: 28
I try not to use GOTO commands as much as possible, and stick to flows like IFs and Elses, and loops. But sometimes GOTO is just necessary. What are your thoughts, is it best to not use GOTO commands?

Here is the hate from StackOverflow http://stackoverflow.com/questions/18863309/the-equivalent-of-a-goto-in-python


Top
   
PostPosted: 02 Feb 2017 10:36 
Offline
Expert

Joined: 23 Dec 2011 13:59
Posts: 3496
You use the tools that the language you are using gives you. Regardless of what language I write in, I try to create the code as structured as possible for readability and understanding. If CMD.EXE had a true function capability like BASH does, I would use a FUNCTION instead.


Top
   
PostPosted: 02 Feb 2017 12:28 
Offline

Joined: 23 Nov 2015 09:34
Posts: 28
@Squashman Agreed, that's why sometimes it's necessary and other times it can be ignored.


Top
   
PostPosted: 02 Feb 2017 12:41 
Offline
Expert

Joined: 22 Jan 2010 18:01
Posts: 2555
Location: Germany
Cornbeetle wrote:
What are your thoughts, is it best to not use GOTO commands?

Yes. GOTO produces unreadable and unmaintainable "Spaghetti Code". Like a spaghetti in a tomato sauce you can't retrace what way it takes and where it ends. Rather use CALL to execute subroutines and return to the point where it was called afterwards.

There are only three reasons for a use of GOTO.
1) Generate loops (like "do-while" loops of other languages)
2) Quit the execution of a batch code or subroutine using GOTO :EOF
3) Quickly escape (break out of a FOR loop or jump to an error handler before you quit the execution)

Steffen


Top
   
PostPosted: 02 Feb 2017 13:06 
Offline

Joined: 24 Jun 2013 17:10
Posts: 422
Location: Bulgaria
aGerman wrote:
Cornbeetle wrote:
What are your thoughts, is it best to not use GOTO commands?

Yes. GOTO produces unreadable and unmaintainable "Spaghetti Code". Like a spaghetti in a tomato sauce you can't retrace what way it takes and where it ends. Rather use CALL to execute subroutines and return to the point where it was called afterwards.

There are only three reasons for a use of GOTO.
1) Generate loops (like "do-while" loops of other languages)
2) Quit the execution of a batch code or subroutine using GOTO :EOF
3) Quickly jump to an error handler before you quit the execution

Steffen


Or breaking for loops or if conditions ...


Top
   
PostPosted: 02 Feb 2017 13:28 
Offline
Expert

Joined: 22 Jan 2010 18:01
Posts: 2555
Location: Germany
Fair enough :wink: I changed my statement accordingly.

Steffen


Top
   
PostPosted: 02 Feb 2017 15:17 
Offline

Joined: 23 Nov 2015 09:34
Posts: 28
aGerman wrote:
Cornbeetle wrote:
What are your thoughts, is it best to not use GOTO commands?


There are only three reasons for a use of GOTO.
1) Generate loops (like "do-while" loops of other languages)
2) Quit the execution of a batch code or subroutine using GOTO :EOF
3) Quickly escape (break out of a FOR loop or jump to an error handler before you quit the execution)

Steffen


GREAT example for why you would need to use a GOTO.
Thanks.


Top
   
PostPosted: 04 Feb 2017 12:25 
Offline

Joined: 04 Feb 2017 12:07
Posts: 2
As a beginner, I find it quite useful. With all the reasons provided above, it seems reasonable that it isn't a very good command to use.


Top
   
PostPosted: 04 Feb 2017 15:35 
Offline

Joined: 06 Sep 2013 21:28
Posts: 640
Location: Virginia, United States
Batch predates the mechanisms by which you can easily avoid a goto. DOS is exempt from this "never use a goto" nonsense, and batch is grandfathered in, imo.

Besides, using a goto doesn't automatically mean you've written bad code, it just makes it easier to write bad code, just like if you used Java or PHP for basically anything.


Top
   
PostPosted: 04 Feb 2017 17:41 
Offline
Expert

Joined: 22 Jan 2010 18:01
Posts: 2555
Location: Germany
Koala wrote:
As a beginner, I find it quite useful.

That's another reason why it is dangerous. GOTO will destroy your programming style and - as ShadowThief already wrote - tempts to write bad code. Almost all modern languages don't even support GOTO. If you never learned living without it you'll get into trouble learning another language.

Just an example where you need GOTO (due to missing do-while loops) and where you don't need it (to conditionally execute different code).
Code: Select all
@echo off &setlocal

:loop
set "input="
set /p "input=Enter y or n: "
if /i "%input%"=="y" (
  call :yes
) else if /i "%input%"=="n" (
  call :no
) else goto loop

echo Back in the main code.
pause
exit /b

:yes
echo Your code here if y was entered.
exit /b

:no
echo Your code here if n was entered.
exit /b


Steffen


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 10 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: xhai and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited