1

All the searching I've tried, and I cant be alone on this, I fail to find proper instructions on how to indent code, place labels in certain points relative to code and/or general do's and don'ts of making your code legible.

Currently my coding is either a random array of attempted indentation, or flat line coding with no indents at all.

Does anybody know any links, general rules or examples themselves on how to stop spaghetti coding and monotonous coding?

Bloodied
  • 1,004
  • 7
  • 20
  • The only language where indentation _actually_ matters that I'm aware of is Python. Just keep at it, and eventually you'll develop your own style. – SomethingDark Oct 19 '15 at 18:43
  • rather post your code here - http://codereview.stackexchange.com/ – npocmaka Oct 19 '15 at 18:43
  • Indents are great for visually organizing the code, I always [use them](http://stackoverflow.com/a/33179295/3959875). Also consider separating the code into several batch files. – wOxxOm Oct 19 '15 at 18:53
  • The recommendations for coding style given at [this post](http://javascript.crockford.com/code.html) can also be used in Batch files. – Aacini Oct 19 '15 at 19:28
  • but with the caveat that batch doesn't support in-line comments. – SomethingDark Oct 19 '15 at 20:06
  • @SomethingDark: Do you refer to a comment in the same line of a command `set "var=value" %Like this one?%` – Aacini Oct 19 '15 at 21:17
  • I suppose you could do that. You could also do something like `set "var=value"&REM This is a comment.` But `rem` statements by themselves need to be at the start of lines. – SomethingDark Oct 19 '15 at 21:22
  • I'm voting to close this question as off-topic because it is about general coding practices rather than a specific error. – Bloodied Oct 20 '16 at 15:15

2 Answers2

1

One of the things that I picked up a few years ago was to be generous with your comments. Not only for yourself, but also for anyone else who'll review your code. I would typically only insert a comment at the beginning of the file to describe what the primary function of the batch file is for, and some of the functions, just to explain what's going to happen, or what's going on. As far as indentation, typically should indent things such as an IF or FOR. Here's a quick example. :: Check username of currently logged in user.

@ECHO OFF

:: Declare variables
SET Something=abc
SET SomethingElse=def
SET FinalNumber=20

:: Start of batch file

:GetUsername
:: Get username of currently logged in user, and output to file
CLS
ECHO %USERNAME%>Test.txt

:CheckAdmin
:: Check if username = Admin, Echo numbers in a FOR loop
CLS
IF "%USERNAME%" EQU "Admin" (
    ECHO Admin is currently logged in
    ECHO Printing numbers 0-%FinalNumber%
    FOR /L %%I IN (0,1,%FinalNumber%) DO (
        ECHO %%I
        ECHO %%I>>Test.txt
    )
) ELSE (
    ECHO Admin is not logged in
    ECHO Printing numbers %FinalNumber%-0
    FOR /L %%I IN (%FinalNumber%,-1,0) DO (
        ECHO %%I
        ECHO %%I>>Test.txt
    )
)
:: Wait 5 seconds, than loop through again, because why not?
ECHO %Something%
ECHO %SomethingElse%
TIMEOUT>NUL /T 5
GOTO :GetUsername

While I'm no "Expert", hopefully this helps clear up a few things for you. As you can see, I prefer to use :: versus REM for comments, and I also set labels :GetUsername on the various functions. Some will say this is right, this is wrong, I feel just go with what you're comfortable with.

Potato Head
  • 143
  • 1
  • 8
  • 1
    I suggest you to not use double-colons for comments. There is not a single reason to do it, but a couple reasons for don't! Further details at http://stackoverflow.com/questions/16632524/what-does-double-colon-mean-in-dos-batch-files/16639875#16639875 – Aacini Oct 19 '15 at 21:09
  • First, thanks Blizfrost, I'll be sure to use more comments and not focus on indenting **everything**. Aacini i read that post, but i failed to see why ":: Comment" is bad, it just went over how any invalid label is ignored and how :: is popular for no reason, along with proof that you cant accidentally "goto :one" if "::one" is a comment. – Bloodied Oct 21 '15 at 15:25
  • 1
    Not a problem, and allegedly according to [This](http://www.robvanderwoude.com/comments.php) if you insert a `::` comment in certain spots of the code, it'll cause a syntax error. While I do like comments, I usually stick to a comment right before or right after a label. And no, there's no need to indent everything, but given a lack of provided code, I couldn't show you where I'd indent on your code, and where I wouldn't. As I stated, you should do fine with just indenting code inside of `IF` & `FOR`. – Potato Head Oct 21 '15 at 18:54
1

I'd say, what you could do, is you could put some parts as one bit, like this:

@echo off
echo Hello. Type in your name.
set /p "name"
if %name%==bob123 goto login
if %name%==joe321 goto login

Then, more parts in another bit.

Floern
  • 33,559
  • 24
  • 104
  • 119
  • Ha, late as it may be, but I've (hopefully) gotten past my bad habits and developed a (hopefully) readable coding style. But i fail to see how that answer works, no comment is contained, through a command `rem`, invalid label `::`, or a skip with `goto :label`. As well, you forgot the `=` sign in your `set /p` command. – Bloodied Mar 28 '16 at 17:45