1.

Solve : Avoiding Spaghetti Code?

Answer»

I've been READING how to avoid spaghetti code in batch files(http://www.robvanderwoude.com/goto.php).

In the example of what spaghetti code is, I realized that the batch file that I use when I logon almost fits this example. Could someone please help me make my batch file more robust, and not have spaghetti code?

Code: [Select]@ECHO OFF
CLS


:MENU
echo WELCOME %USERNAME%

echo 1 - Start KeePass
echo 2 - Backup
echo 3 - FireFox
echo 4 - Exit

SET /P M=Please Enter Selection, then Press Enter:

IF %M%==1 GOTO StarKeePass
IF %M%==2 GOTO Backup
IF %M%==3 GOTO FireFox
IF %M%==4 GOTO :EOF
GOTO MENU


:StarKeePass
SET keePass="%USERPROFILE%\KeePass\KeePass-2.30\KeePass.exe"
SET kdb="%USERPROFILE%\KeePass\PasswordDatabase\PasswordDatabase.kdbx"

echo I'll start KeePass for You
START "" %keePass% %kdb%

GOTO MENU

:Backup
SET backup="%USERPROFILE%\backup.bat"
call %backup%

GOTO MENU

:FireFox
cd "C:\Program Files (x86)\Mozilla Firefox\"
start firefox.exe

GOTO MENUThat page at Rob Van Der Woude's SITE showed you how to bring a certain amount of structure to writing batch code, but I think you have to face that batch scripts are always going to be spaghetti. There are other languages you can use.Just a few examples are Python, Perl, TCL, Ruby, and even Visual basic Script, which is native to Windows.
Salmon Trout id right. Batch was intended for limited use.
Microsoft encourages the use of better tools, even for ADMINISTRATORS who do some small amount of code writing. The use of either Windows Scrip or Power Shell is now what administrators do to controls a Windows network.

Quote

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework.
-Google

What is Windows PowerShell? -stackoverflow..
Quote
The language is sane, especially when compared to CMD.

Does that help?
Your code can be changed, and made a little more robust - is it less spaghetti-like ? Dunno.

Code: [Select]@ECHO OFF
CLS
:MENU
echo Welcome %USERNAME%

echo 1 - Start KeePass
echo 2 - Backup
echo 3 - FireFox
echo 4 - Exit
echo(

SET "keePass=%USERPROFILE%\KeePass\KeePass-2.30\KeePass.exe"
SET "kdb=%USERPROFILE%\KeePass\PasswordDatabase\PasswordDatabase.kdbx"
SET "backup=%USERPROFILE%\backup.bat"
set "m="
SET /P "M=Please Enter Selection, then Press Enter: "
IF "%M%"=="1" (echo I'll start KeePass for You & START "" "%keePass%" "%kdb%")
IF "%M%"=="2" (call "%backup%")
IF "%M%"=="3" (start "" /d "C:\Program Files (x86)\Mozilla Firefox\" firefox.exe)
IF "%M%"=="4" (GOTO :EOF)
GOTO MENU
When it comes to batches. They are usually a down and dirty method of achieving something without writing it up from scratch. Its really intended for scripted routines of file interaction and scheduled tasks etc and system admin etc but never distributed and/or compiled as actual software etc.

I have always lived with the fact that batches require goto's to jump around and as long as it does as is intended, so be it.

If you were programming in a somewhat modern or evolved to modern language other than batch such as C++, Python, Perl, VB, Java, and on and on. You would be creating loops and calling to objects for functions etc for reuse vs having to rewrite the code for each part of the FLOWCHART that requires the specific object to function at. Lots of these languages do have legacy support for goto operation. I have sometimes with C++ instead of having to make changes adding a loop add a goto statement that restarts the program from the beginning if I get lazy. Once you compile it, and it does as intended, then your good to go. However if you were programming for a client and gave them the source code in the end, you might not get future work with that company if you use goto statements as well as not enough comment lines.



Discussion

No Comment Found