| 1. |
Solve : Avoiding Spaghetti Code? |
|
Answer» I've been READING how to avoid spaghetti code in batch files(http://www.robvanderwoude.com/goto.php). 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. |
|