Page 1 of 1

So much hate for the GOTO command, why?

Posted: 02 Feb 2017 09:42
by Cornbeetle
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

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

Posted: 02 Feb 2017 10:36
by Squashman
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.

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

Posted: 02 Feb 2017 12:28
by Cornbeetle
@Squashman Agreed, that's why sometimes it's necessary and other times it can be ignored.

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

Posted: 02 Feb 2017 12:41
by aGerman
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

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

Posted: 02 Feb 2017 13:06
by npocmaka_
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 ...

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

Posted: 02 Feb 2017 13:28
by aGerman
Fair enough :wink: I changed my statement accordingly.

Steffen

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

Posted: 02 Feb 2017 15:17
by Cornbeetle
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.

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

Posted: 04 Feb 2017 12:25
by Koala
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.

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

Posted: 04 Feb 2017 15:35
by ShadowThief
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.

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

Posted: 04 Feb 2017 17:41
by aGerman
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