So much hate for the GOTO command, why?
Moderator: DosItHelp
-
- Posts: 31
- Joined: 23 Nov 2015 09:34
So much hate for the GOTO command, why?
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
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?
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.
-
- Posts: 31
- Joined: 23 Nov 2015 09:34
Re: So much hate for the GOTO command, why?
@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?
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?
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?
Fair enough I changed my statement accordingly.
Steffen
Steffen
-
- Posts: 31
- Joined: 23 Nov 2015 09:34
Re: So much hate for the GOTO command, why?
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?
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.
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: So much hate for the GOTO command, why?
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.
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?
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