| View previous topic :: View next topic |
| Author |
Message |
jonny
Joined: 13 Nov 2004 Posts: 3005 Location: Minnesota
|
Posted: Thu Feb 24, 2005 9:30 pm Post subject: Unofficial Syntax and Style Standard |
|
|
Toralf recently suggested a mini-"Tidy" script to clean up poorly structured AutoHotkey scripts. Before this can be done, though, a standard needs to be set down for syntax and style in AHK scripts. I'm starting a separate thread for this because I think that once it's developed it will be a useful resource in and of itself.
I understand that it might not be Chris' wish to have a set standard like this, so I'm stressing that it's unofficial.
I'd suggest that for the most fundamental style rules, we draw from the showcase scripts, since many have learned from example by them and it might be difficult to migrate to a different standard. As for how exactly this standard will be structured, I will maintain the list below as the thread develops, and it will only contain rules which have been firmly agreed upon by a majority. It can also contain "non-rules," which specify areas of style for which no standard can or should be made.
- No standards have been set yet.
Last edited by jonny on Thu Feb 24, 2005 9:39 pm; edited 1 time in total |
|
| Back to top |
|
 |
jonny
Joined: 13 Nov 2004 Posts: 3005 Location: Minnesota
|
Posted: Thu Feb 24, 2005 9:38 pm Post subject: |
|
|
I didn't want to make any of my own suggestions in that first post since I like to have one "master post" in threads like this to maintain something that resembles order. So anyway, here are my first suggestions:
- All statements within blocks (e.g. loops and if statements) should be indented by either one tab or four spaces.
- All flow-control functions that are the direct result of a conditional statement should be indented by either one tab or four spaces.
- Semi-colon comments should have at least one space between the comment flag and the text of the comment.
- Same-line comments should be avoided when the comments are more than 1 1/2 times larger than their corresponding functions.
More to come as I think of them! Here are some areas I'm still trying to reach a decision on:
- The case (upper, lower, mixed) of functions, their parameters, and variables.
- Spaces between parameters.
- Whitespace.
- Whether a comma should be between a function and it's first parameter or not.
- Variable names.
- When using /* */ is acceptable.
Note that for some of these, I'm considering "non-rules" too. |
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10464
|
Posted: Thu Feb 24, 2005 10:27 pm Post subject: Re: Unofficial Syntax and Style Standard |
|
|
| jonny wrote: | | I understand that it might not be Chris' wish to have a set standard like this, so I'm stressing that it's unofficial. | Since formatting is a very personal thing, I think it is possible to have the best of both worlds by providing options inside this "script beautifier":
Tabs vs. spaces (if if spaces, how many)
Whether to have spaces after the comma in each parameter (as you said).
Whether to omit the first comma (as you said).
How to capitalize command names and possibly keywords (as you said).
| Quote: | | Same-line comments should be avoided when the comments are more than 1 1/2 times larger than their corresponding functions. | I tend to disagree. For example, these seem fine to me:
WinActivate ; Activate the window found by the command above.
Loop ; For each tab-delimited field in the current line:
...
Also, I prefer two or more spaces in front of "same line" comments because it makes it more clear (in the absence of syntax coloring) that they're comments and not part of the command.
Great ideas so far.
A related goal might be a way to convert a script into a colorized HTML version, where commands are in one color, comments in another, variable names in a third, and so on. This allows a script to be "published" in a more viewer friendly format. |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5009 Location: imaginationland
|
Posted: Thu Feb 24, 2005 10:45 pm Post subject: |
|
|
Good guidelines for newbies but I think people can layout their scripts however, just as long it as does the job right  _________________
RegExReplace("irc.freenode.net/autohotkey", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2") |
|
| Back to top |
|
 |
jonny
Joined: 13 Nov 2004 Posts: 3005 Location: Minnesota
|
Posted: Thu Feb 24, 2005 11:23 pm Post subject: |
|
|
The options sound like a very good idea and I will try to do that; however, it would still be nice if we laid down some kind of standard that's generally accepted but that certainly doesn't have to be conformed to. With such a standard, we'd then have a basis for more "formal" scripts like ones in the showcase or in a future wiki.
| Quote: | I tend to disagree. For example, these seem fine to me:
WinActivate ; Activate the window found by the command above.
Loop ; For each tab-delimited field in the current line: |
Yes, those are good exceptions; I carefully chose the word "avoided" instead of "not be used" to indicate that it was only a general rule and does not apply to everything.
| Quote: | | Also, I prefer two or more spaces in front of "same line" comments because it makes it more clear (in the absence of syntax coloring) that they're comments and not part of the command. |
I actually meant the space between the semi-colon (or other comment flag) and the text (i.e. This command helps blah blah blah...); or did you mean this too? Anyway, the space between the command and the comment in full is entirely a matter of preference, I understand.
| Quote: | | A related goal might be a way to convert a script into a colorized HTML version, where commands are in one color, comments in another, variable names in a third, and so on. This allows a script to be "published" in a more viewer friendly format. |
Great idea. I guess I'll have to read up on my html. Do you think I should also include a bbcode option, to format it for use inside a quote tag? (For those who post source in the forum directly instead of hosting it) |
|
| Back to top |
|
 |
BoBo Guest
|
Posted: Thu Feb 24, 2005 11:47 pm Post subject: |
|
|
| Quote: | | A related goal might be a way to convert a script into a colorized HTML version, where commands are in one color, comments in another, variable names in a third, and so on. This allows a script to be "published" in a more viewer friendly format. | I seem to remember that this has been already requested in the Forum, but hasn't made it to become a final script. The problem was about this:
| Quote: | Loop, Read, ...
{
MsgBox, %A_LoopReadLine%
} |
As Loop is contained in A_LoopReadLine the colored replacement hasn't worked out properly.
 |
|
| Back to top |
|
 |
BoBo Guest
|
Posted: Thu Feb 24, 2005 11:55 pm Post subject: |
|
|
BoBo wrote:
| Quote: | | I seem to remember that this has been already requested in the Forum | BoBo used his brain. Bingo! ---> [here] Its requester: the bobo BoBo  |
|
| Back to top |
|
 |
jonny
Joined: 13 Nov 2004 Posts: 3005 Location: Minnesota
|
Posted: Fri Feb 25, 2005 12:22 am Post subject: |
|
|
These are the key words here:
| Quote: | | but hasn't made it to become a final script. |
I'll give it a go and see what I can do. The first priority for this AHK-Tidy, though, is to format scripts according to the options set. |
|
| Back to top |
|
 |
jonny
Joined: 13 Nov 2004 Posts: 3005 Location: Minnesota
|
Posted: Fri Feb 25, 2005 3:12 am Post subject: |
|
|
| Does anyone else have style suggestions? If not for anything else, they could at least be used as defaults for the formatter. |
|
| Back to top |
|
 |
Rajat
Joined: 28 Mar 2004 Posts: 1717
|
Posted: Fri Feb 25, 2005 4:38 am Post subject: |
|
|
i started work on it long ago (here)... but scrapped what i had done bcoz nobody showed interest! _________________
 |
|
| Back to top |
|
 |
jonny
Joined: 13 Nov 2004 Posts: 3005 Location: Minnesota
|
Posted: Fri Feb 25, 2005 4:45 am Post subject: |
|
|
Is there anything left that you could post? (If so, make a new thread) I had considered doing it in a different language, especially because of the current lack of regex in AHK, but if you already have something it couldn't be too hard to work from there.
As for the html highlighting, I think that will definitely have to be done in a different language. There's too much to be done for syntax highlighting to do it with AHK's limited string capabilities.  |
|
| Back to top |
|
 |
Invalid User
Joined: 14 Feb 2005 Posts: 442 Location: Texas, Usa
|
Posted: Fri Feb 25, 2005 5:51 am Post subject: |
|
|
Just my two cents, I ALWAYS have spaces between my commas, always have the command and first param seperated by a comma, comments are never more than one whitespace from the end of the command line, there is never a space between the semi-colon and the comment, All the contents of a block is 1 tab in(which in my case just so happens to be 4 spaces). Oh yeah I tend not to let my comments run off the width of the editor, in that case I always start a new line. and always keep a empty line above and below comments that are about a process in gerneral rather than same line comments that talk about the command the commented line line is on. Sample | Code: |
Gui, Add, Button, x%Xopt% y%Yopt%, Text ;Comment....
;The next three lines do....
Xopt += 5 ;Adds five to the X option
Xopt -2 ;Subtracts 2 from the X option
NewOpt := (Xopt + Yopt) ;Add both options together
Loop
{
;LoopContents
}
| er somthing like that _________________ my lame sig  |
|
| Back to top |
|
 |
Rajat
Joined: 28 Mar 2004 Posts: 1717
|
Posted: Fri Feb 25, 2005 6:07 am Post subject: |
|
|
@jonny
i might be having some of them... but they're old and i don't remember what i did then... but i'll post when i'll get to my PC.
@IU
very good practices! ... i also do much the same. i also write cmds like FileAppend and not fileappend. makes it more readable. _________________
 |
|
| Back to top |
|
 |
Invalid User
Joined: 14 Feb 2005 Posts: 442 Location: Texas, Usa
|
Posted: Fri Feb 25, 2005 6:58 am Post subject: |
|
|
Might you give a short example of this file appending practice? _________________ my lame sig  |
|
| Back to top |
|
 |
Rajat (cookie trouble) Guest
|
Posted: Fri Feb 25, 2005 7:02 am Post subject: |
|
|
| sorry for not being clear, i just meant that i capitalize the first letter of words in cmds...like IfEqual or StringReplace etc. |
|
| Back to top |
|
 |
|