So much hate for the GOTO command, why?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Cornbeetle
Posts: 31
Joined: 23 Nov 2015 09:34

So much hate for the GOTO command, why?

#1 Post by Cornbeetle » 02 Feb 2017 09:42

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

Squashman
Expert
Posts: 3699
Joined: 23 Dec 2011 13:59

Re: So much hate for the GOTO command, why?

#2 Post by Squashman » 02 Feb 2017 10:36

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.

Cornbeetle
Posts: 31
Joined: 23 Nov 2015 09:34

Re: So much hate for the GOTO command, why?

#3 Post by Cornbeetle » 02 Feb 2017 12:28

@Squashman Agreed, that's why sometimes it's necessary and other times it can be ignored.

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

Re: So much hate for the GOTO command, why?

#4 Post by aGerman » 02 Feb 2017 12:41

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

npocmaka_
Posts: 448
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: So much hate for the GOTO command, why?

#5 Post by npocmaka_ » 02 Feb 2017 13:06

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 ...

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

Re: So much hate for the GOTO command, why?

#6 Post by aGerman » 02 Feb 2017 13:28

Fair enough :wink: I changed my statement accordingly.

Steffen

Cornbeetle
Posts: 31
Joined: 23 Nov 2015 09:34

Re: So much hate for the GOTO command, why?

#7 Post by Cornbeetle » 02 Feb 2017 15:17

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.

Koala
Posts: 2
Joined: 04 Feb 2017 12:07

Re: So much hate for the GOTO command, why?

#8 Post by Koala » 04 Feb 2017 12:25

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.

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

Re: So much hate for the GOTO command, why?

#9 Post by ShadowThief » 04 Feb 2017 15:35

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.

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

Re: So much hate for the GOTO command, why?

#10 Post by aGerman » 04 Feb 2017 17:41

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

Post Reply