AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Unofficial Syntax and Style Standard
Goto page 1, 2  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> General Chat
View previous topic :: View next topic  
Author Message
jonny



Joined: 13 Nov 2004
Posts: 3005
Location: Minnesota

PostPosted: Thu Feb 24, 2005 9:30 pm    Post subject: Unofficial Syntax and Style Standard Reply with quote

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. Exclamation


Last edited by jonny on Thu Feb 24, 2005 9:39 pm; edited 1 time in total
Back to top
View user's profile Send private message
jonny



Joined: 13 Nov 2004
Posts: 3005
Location: Minnesota

PostPosted: Thu Feb 24, 2005 9:38 pm    Post subject: Reply with quote

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:

  1. All statements within blocks (e.g. loops and if statements) should be indented by either one tab or four spaces.
  2. All flow-control functions that are the direct result of a conditional statement should be indented by either one tab or four spaces.
  3. Semi-colon comments should have at least one space between the comment flag and the text of the comment.
  4. 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
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10464

PostPosted: Thu Feb 24, 2005 10:27 pm    Post subject: Re: Unofficial Syntax and Style Standard Reply with quote

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
View user's profile Send private message Send e-mail
Titan



Joined: 11 Aug 2004
Posts: 5009
Location: imaginationland

PostPosted: Thu Feb 24, 2005 10:45 pm    Post subject: Reply with quote

Good guidelines for newbies but I think people can layout their scripts however, just as long it as does the job right Razz
_________________

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
View user's profile Send private message Visit poster's website
jonny



Joined: 13 Nov 2004
Posts: 3005
Location: Minnesota

PostPosted: Thu Feb 24, 2005 11:23 pm    Post subject: Reply with quote

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. Smile 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
View user's profile Send private message
BoBo
Guest





PostPosted: Thu Feb 24, 2005 11:47 pm    Post subject: Reply with quote

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.

Crying or Very sad
Back to top
BoBo
Guest





PostPosted: Thu Feb 24, 2005 11:55 pm    Post subject: Reply with quote

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 Shocked
Back to top
jonny



Joined: 13 Nov 2004
Posts: 3005
Location: Minnesota

PostPosted: Fri Feb 25, 2005 12:22 am    Post subject: Reply with quote

These are the key words here:

Quote:
but hasn't made it to become a final script.


Very Happy

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
View user's profile Send private message
jonny



Joined: 13 Nov 2004
Posts: 3005
Location: Minnesota

PostPosted: Fri Feb 25, 2005 3:12 am    Post subject: Reply with quote

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
View user's profile Send private message
Rajat



Joined: 28 Mar 2004
Posts: 1717

PostPosted: Fri Feb 25, 2005 4:38 am    Post subject: Reply with quote

i started work on it long ago (here)... but scrapped what i had done bcoz nobody showed interest!
_________________
Back to top
View user's profile Send private message
jonny



Joined: 13 Nov 2004
Posts: 3005
Location: Minnesota

PostPosted: Fri Feb 25, 2005 4:45 am    Post subject: Reply with quote

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. Confused
Back to top
View user's profile Send private message
Invalid User



Joined: 14 Feb 2005
Posts: 442
Location: Texas, Usa

PostPosted: Fri Feb 25, 2005 5:51 am    Post subject: Reply with quote

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 Smile
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger
Rajat



Joined: 28 Mar 2004
Posts: 1717

PostPosted: Fri Feb 25, 2005 6:07 am    Post subject: Reply with quote

@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
View user's profile Send private message
Invalid User



Joined: 14 Feb 2005
Posts: 442
Location: Texas, Usa

PostPosted: Fri Feb 25, 2005 6:58 am    Post subject: Reply with quote

Might you give a short example of this file appending practice?
_________________
my lame sig Smile
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger
Rajat (cookie trouble)
Guest





PostPosted: Fri Feb 25, 2005 7:02 am    Post subject: Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> General Chat All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group