your personal AutoHotkey style guide

Discuss Autohotkey related topics here. Not a place to share code.
Forum rules
Discuss Autohotkey related topics here. Not a place to share code.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: your personal AutoHotkey style guide

13 Apr 2018, 15:10

nnnik wrote:AHK studio does most of it for me
At some point I'll have to take a closer look at this. Ty.
- Funnily enough I find that o/v really help with readability, it's immediate, you know that what you're looking is an object/variable and not something else.
Regular vs object variables are easily distinguished by the (lack of) object syntax, eg, myvar.stuff. The documentation mentions prefixes here.
I almost never pass a quoted string to dllcall, so in general the quotes helps with readability imo, due to syntax highlighting

Code: Select all

dllcall("func", "ptr", var, "int", var2)
; vs
dllcall("func", ptr, var, int, var2)
Readability aside, it is a very odd feature to allow this.
jeeswg wrote:

Code: Select all

vRet := !!DllCall("user32\DestroyIcon", Ptr,DllCall("user32\CopyIcon", Ptr,hIcon, Ptr))
vRet := !!DllCall("user32\DestroyIcon", "Ptr", DllCall("user32\CopyIcon", "Ptr", hIcon, "Ptr"))
Horizontal space clearly isn't a big factor.
jeeswg wrote:word wrap
What editor do you use, if it is notepad, please lie :lol:

Cheers.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: your personal AutoHotkey style guide

13 Apr 2018, 15:26

- Actually, that's an interesting point Helgef, a good style is a good style in Notepad, that is not dependent on syntax highlighting etc.
- You never know when the raw code is going to appear as plaintext on some website, or even, in some Google/Bing search results I saw today. Eek.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: your personal AutoHotkey style guide

13 Apr 2018, 15:34

I'd copy it into my editor if I wanted to look at it... :roll:
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: your personal AutoHotkey style guide

13 Apr 2018, 16:41

You are completely beside the point.
Image
Recommends AHK Studio
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: your personal AutoHotkey style guide

13 Apr 2018, 19:51

What editor do you use, if it is notepad, please lie
a good style is a good style in Notepad, that is not dependent on syntax highlighting etc.
Please, have someone tell me that he does not use Notepad. :wtf: :shock: :eh: :lol:

Code: Select all

;YES
;comment
;NO
; comment

;Indentation style - Wikipedia
;https://en.wikipedia.org/wiki/Indentation_style

;YES (Allman style)
Loop 3
{
    MsgBox
    Loop 3
    {
        InputBox Input
    }
}
;NO (K&R: one true brace)
Loop 3 {
    MsgBox
    Loop 3 {
        InputBox Input
    }
}

;YES
if (var1 = var2)
;IT DEPENDS (But almost never)
if ( var1 = var2 )

;YES
oArray := ["a", "b", "c"]
;IT DEPENDS (But almost never)
oArray := ["a","b","c"]
;NO
oArray := [ "a", "b", "c" ]

;wtf is this
DllCall("MessageBox", Ptr,0, Str,"text", Str,"title", UInt,0)
;NO
DllCall( "MessageBox", "Ptr", 0, "Str", "text", "Str", "title", "UInt", 0 )
;NO
DllCall("MessageBox", "Ptr", 0, "Str", "text", "Str", "title", "UInt", 0)
;YES
DllCall("DllFile.dll\Function", "Ptr", 0, "Str", "text", "Str", "title", "UInt", 0)

;NO
MsgBox var "text" var var "text" "text" var "text"
;YES
MsgBox var . "text" . var . var . "text" . "text" . var . "text"

;NO (initial commas) (I do not use AHKv1. Still, I do not like the initial coma)
Loop, Parse, vText, `n, `r
;YES (no initial commas)
Loop Parse, Text, `n, `r

;NO (initial commas)
MouseGetPos, vCurX, vCurY, hWnd, vCtlClassNN
MouseGetPos,,, hWnd, vCtlClassNN
;YES (no initial commas)
MouseGetPos vCurX, vCurY, hWnd, vCtlClassNN
MouseGetPos ,, hWnd, vCtlClassNN

;NO
vText
oArray
;YES
text
array
xxx_xxx
; ... good
camelCaseForVars
CamelCaseForClasses

;YES
if !(var1 = var2)
if (var1 == "")
;NO
if not (var1 = var2)
if (var1 = "")
if var1 = var2

;YES
if (var1 && var2)
if (var1 || var2)
;NO
if (var1 AND var2)
if (var1 OR var2)

;OBVIOUSLY YES
var := "text"
var1 := var2
;OBVIOUSLY NO (NOTE: NOT AVAILABLE IN AHK V2)
var = text
var1 = %var2%

;NO ...
vText := "line 1`r`n" "line 2`r`n" "line 3"
;YES (the 'n' combines with the string)
Text := "line 1`r`nline 2`r`nline 3"

;cant recall the last time i had to put 2 consecutive spaces - me neither x2
;??
vText := A_Space A_Space
;??
vText := "  "

;NO (indentation of 1 tab) (TAB? inconceivable)
if (var1 = var2)
    MsgBox    ; Oops! Sublime Text has replaced me TAB for spaces.
;strongly YES (indentation of 4 spaces)
if (var1 = var2)
    MsgBox

;AMBIGUOUS CASES ...

;YES
if (var1 = var2)
    MsgBox
;NO (UNNECESSARY CURLY BRACES)
if (var1 = var2)
{
    MsgBox
}
if (var1 = var2) {
    MsgBox
}

;IT DEPENDS
var := 1 + 1
;IT DEPENDS
var := 1+1
:wave:
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: your personal AutoHotkey style guide

14 Apr 2018, 08:58

jeeswg wrote:

Code: Select all

;YES (initial commas)
Loop, Parse, vText, `n, `r
;NO (no initial commas)
Loop Parse, vText, `n, `r

;YES (initial commas)
MouseGetPos, vCurX, vCurY, hWnd, vCtlClassNN
MouseGetPos,,, hWnd, vCtlClassNN
;NO (no initial commas)
MouseGetPos vCurX, vCurY, hWnd, vCtlClassNN
MouseGetPos ,, hWnd, vCtlClassNN
Is there some difference between
  • (not)using initial commas with Loop command (Loop, Parse...) and
  • (not)using them with regular commands (MouseGetPos)
?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: your personal AutoHotkey style guide

14 Apr 2018, 09:05

There should be no difference in functionality, it's simply that I find the initial comma style better-looking and clearer re. omitting the first parameter.

Code: Select all

;YES (initial commas)
Cmd, Arg1, Arg2
Cmd,, Arg2
;NO (no initial commas)
Cmd Arg1, Arg2
Cmd , Arg2
;function (for comparison)
Fnc(Arg1, Arg2)
Fnc(, Arg2)
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: your personal AutoHotkey style guide

14 Apr 2018, 09:17

I just realized that this is permissive in AHKv2 :shock:

Code: Select all

DllCall("MessageBox", Ptr,0, Str,"text", Str,"title", UInt,0)
It only brings tremendous confusion and bad practice.
btw, would be great f(ByRef p)`n{`n} --> f(&p)`n{`n}. I would definitely choose &. It would save a lot of space.
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: your personal AutoHotkey style guide

14 Apr 2018, 09:24

Flipeador wrote:...
Your style is very interesting in some points!

Code: Select all

;NO
DllCall("MessageBox", "Ptr", 0, "Str", "text", "Str", "title", "UInt", 0)
;YES
DllCall("DllFile.dll\Function", "Ptr", 0, "Str", "text", "Str", "title", "UInt", 0)
Above: As I understand, you prefer to manually specify the file from which the function will be taken?

Code: Select all

;YES
if (var1 == "")
;NO
if (var1 = "")
if var1 = var2
Above:
  • For what reason you prefer if (var1 == "") over if (var1 = "")? Why you prefer case-sensitive-equal here?
  • I see you discourage about using if var1 = var2, but it's not very clear what you mean, because it doesn't seem to be an expression... Hm!
; So many things to learn!
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: your personal AutoHotkey style guide

14 Apr 2018, 09:32

john_c wrote:As I understand, you prefer to manually specify the file from which the function will be taken?
Yes, for clarity. sincerely, i do not know if this affects the performance, I have not looked at the source code or done tests, but I do not think so.
For what reason you prefer if (var1 == "") over if (var1 = "")? Why you prefer case-sensitive-equal here?
I just like more ==, like in C++. I use it with "" and numbers. Also, I think it should be faster.
I see you discourage about using if var1 = var2, but it's not very clear what you mean, because it doesn't seem to be an expression
It was just an example, I know, I just want to say that I do not agree to omit the parentheses. I prefer many parentheses, in case of not being readable, I add spaces or use a new line.
:)
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: your personal AutoHotkey style guide

14 Apr 2018, 10:58

Was not discussed here yet: objects.

From my point of view:

Code: Select all

; Bad. Key1 and Key2 are strings, and for that reason we should write them quoted
Obj := {Key1: "Value1", Key2: "Value2"}

; Good
Obj := {"Key1": "Value1", "Key2": "Value2"}
Retrieving values from objects, from my point of view:

Code: Select all

object := {"foo": "aaa", "bar": "bbb", "foo bar": "ccc ddd"}

; Bad, because it's not possible to retrieve the value of multiword key (foo bar)
msgBox % object.foo

; Good, for the opposite reason
msgBox % object["foo"]
msgBox % object["foo bar"]
What you think about it, guys?
Last edited by john_c on 14 Apr 2018, 11:26, edited 1 time in total.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: your personal AutoHotkey style guide

15 Apr 2018, 02:48

Some additional notes on v2 style
Flipeador wrote:

Code: Select all

;YES (no initial commas)
Loop Parse, Text, `n, `r
In v2, no inital comma is,

Code: Select all

;YES (no initial commas)
Loop Parse Text, "`n", "`r"
Misc.

Code: Select all

; If
; yes
if expr
; no
if (expr)
; For
; yes
for key, val in expr
; no
for(key, val in expr) ; Space between 'for' and '(' also allowed
I always just lower case for control flow statments, eg

Code: Select all

; Yes
loop
for
if
return
goto
; No
Loop
For
If
Return
Goto
LOOP
FOR
iF
; ...
I also prefer == over = when case doesn't matter.

Cheers.

Edit
Flipeador wrote:btw, would be great f(ByRef p)`n{`n} --> f(&p)`n{`n}. I would definitely choose &. It would save a lot of space.
I'd prefer f(p) ->f(ByVal p), that is, byref is default.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: your personal AutoHotkey style guide

08 May 2018, 19:20

Text barriers.
I use:

Code: Select all

;==================================================
;==============================
;====================
;===============
;==========
;=====
;===
;50 and possibly also: 3, 5, 10, 15, 20, 30
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: your personal AutoHotkey style guide

09 May 2018, 15:08

More notation debates to take note of.

Indentation. The big debate:
indentation systems - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=39920

Code: Select all

;YES (text barriers)
;3 / 5 / 10 / 15 / 20 / 30 / 50
;===
;=====
;==========
;===============
;====================
;==============================
;==================================================

;DllCall
;DllCall converter/cleaner (e.g. x32 to x64/x32 two-way compatible) - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=31365&p=173111#p173111[/url]
;there are 6 key options re. DllCall, summarised as: CGKU/.dll/Int/*/"/pairing
;my preferences:
;- dll name: specify comctl32/gdi32/kernel32/user32 even when they can be omitted (NO: omit them)
;- dll name: omit .dll (OK: include .dll)
;- types: omit Int when it's the return type (OK: always show the return type)
;- types: use P e.g. IntP (OK: use * e.g. Int*)
;- types: remove double quotes from parameter types e.g. Int (where possible) (OK: use " e.g. "Int")
;- parameters: group 'type,argument' pairs (NO: 'type, argument')

;YES (omit unnecessary double quotes)
obj := {key1:"value1", key2:"value2"}
;OK
obj := {"key1":"value1", "key2":"value2"}

;YES (always include &)
NumGet(&vData, 0, "UInt")
;NO
NumGet(vData, 0, "UInt")

;YES (constant as comment)
PostMessage, 0x111, 3,,, A ;WM_COMMAND := 0x111
;OK
WM_COMMAND := 0x111
PostMessage, % WM_COMMAND, 3,,, A

;YES (initial semicolons are better for parsing scripts)
;comment line 1
;comment line 2
;NO
/*
comment line 1
comment line 2
*/

;YES (lower-case control flow statements, apart from Loop)
if
return
Loop
;NO
If
Return
loop

;YES (Hungarian notation for handles/pointers)
hBitmap
pBitmap

;YES (variables that I intend to be global)
vGblXXX e.g. vGblDirAhk2

;YES (custom 'A_' variables that I think should be built-in)
AX_XXX e.g. AX_ScriptPID
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: your personal AutoHotkey style guide

19 May 2018, 20:25

- @nnnik: I was interested in these principles. I agree with them, and was wondering whether you'd read anything that led you to use them.
- In theory it might be good to split the parameters across lines, to comment on a particular parameter, although I would instead put the comment above the DllCall/MsgBox etc line.
- Also, you might have a long and complex parameter. So it would help to split the parameters across lines in that case. What would you do in that situation? Would you create a separate variable?

Code: Select all

;NO - never multiple lines are reserved for code structural syntax elements ( e.g. a function or a loop )
DllCall("MessageBox"
	, "Ptr", 0
	, "Str", "text"
	, "Str", "title"
	, "UInt", 0)

;NO - never multiple lines are reserved for code structural syntax elements ( e.g. a function or a loop )
MsgBox, % var . "text" . var . var
	 . "text" . "text" . var
	 . "text"
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Cerberus
Posts: 172
Joined: 12 Jan 2016, 15:46

Re: your personal AutoHotkey style guide

31 May 2018, 14:26

I think giving variables clear names is paramount, as mentioned above. Thinks I consider when naming:
- What does it contain?
- What will it be used for?
In the example of getting the PID of a windows, I might do it like this (pseudo-code):

Code: Select all

GetPID, Active_PID
If (Active_PID = DesiredValue)
    Notepad_PID := Active_PID
ExpandWindow(Notepad_PID)
And I always try to capitalise everything at the beginning of a line or proper names, because I like code to look as much like proper English as possible.
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: your personal AutoHotkey style guide

13 Jun 2018, 12:08

@jeeswg, Hello, since you omit the quotes when declaring the data types in DllCall, I wonder what happens in cases where super-global variables (or classes) have been declared with the same name as a data type (eg. UINT)?. Do not you think it's confusing?.
I'm sorry, but I can not prove anything at this time.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: your personal AutoHotkey style guide

13 Jun 2018, 12:35

- Originally, a long time ago, I thought that AutoHotkey would parse the DllCall lines, and convert Int into "Int" automatically. Since then I don't think that this is the case, and so yes, there are potential (minor?) (major?) disadvantages to omitting the quotes, although clearly readability is the advantage.
- Since I already wrote the code to convert them all, I could potentially convert them all at some point.
DllCall converter/cleaner (e.g. x32 to x64/x32 two-way compatible) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=31365
- HotKeyIt, and I to an extent, have been working on lists of DllCall functions. He added built-in support for a DllCall alternative in AutoHotkey_H, and I have looked at creating a custom function for AutoHotkey_L, however, because my converter script is so easy to use, and because I try to avoid scripts that I share having additional dependencies, it hasn't been a priority to pursue the alternative function route.
C++: DllCall: get parameter types/sizes - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 23&t=39426
WinApi
https://hotkeyit.github.io/v2/docs/commands/WinApi.htm
- Also, lexikos recently added support for creating pointers to strings on the fly. That makes a DllCall alternative, that omits parameter types, more feasible.
AutoHotkey v2 alpha (UPDATES) - Page 3 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 67#p211067
Changed &address-of to work with literal and temporary strings.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: your personal AutoHotkey style guide

13 Jun 2018, 12:46

although clearly readability is the advantage
This seems totally subjective to me, and it depends on the editor you use. In my editor, which supports syntax highlighting, it's just the opposite, quotes are much clearer than without them.
The rest you have written, I do not know what has to do with my comment. In my opinion, the only good thing I rescue from AHK_H is the multi-thread.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: your personal AutoHotkey style guide

13 Jun 2018, 12:51

It is unreadable, even in notepad.
Issue example, probably unlikely, but still,

Code: Select all

global str := 'char'
f
f(){
	dllcall 'MessageBox', 'ptr', 0, str, 'abcde', 'ptr', 0, 'uint', 0x42
}
Hopefully, this will not be allowed in future v2.

Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 4 guests