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.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: your personal AutoHotkey style guide

03 Sep 2018, 04:57

I would choose MyFunc1. Preferences change over time, but the fact that the function returns either one of two value is for my eyes more visible in MyFunc1.

Tomorrow, I might want to emphasize the fact of "default behaviour" being False. Then I might choose MyFunc2, with an extra empty line before the return statement, like I often do for more possible results to choose from.

Code: Select all

If (isSpecialCase1 = True)
    Return, "Bingo"
If (isSpecialCase2 = True)
    Return, "Bongo"

; default
Return, "Bungo"    
I guess, my personal style prefers to put similar Return statements at similar indentation levels.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: your personal AutoHotkey style guide

19 Sep 2018, 18:14

- Often, when writing scripts, I might choose an object's variable name based on what the keys are, e.g. oPID.
- But it can be unclear what values that object contains, so perhaps a better name would be oPIDToHWnd.
- Even with this, the syntax can get quite confusing regarding the order, e.g. hWnd := oPIDToHWnd[vPID].
- But if you were to add an hWnd key for each PID, it seems like overkill, e.g. hWnd := oPID[vPID].hWnd.
- hWnd := oHWndFromPID[vPID] isn't great.
- ObjGetValueByRef(oPIDToHWnd, vPID, hWnd) perhaps. Hmm.
- I wondered what other people thought.
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

21 Sep 2018, 03:12

using To make no sense imo. I'd do it like this, hwnd := hwnds[ pid ], it clearly shows the association between pid and hwnd in a list of hwnds. When you first create the hwnds array, make a comment describing it. Prefixing o or v only clutters the view imo.

Cheers.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: your personal AutoHotkey style guide

21 Sep 2018, 03:28

Or make a function which is descriptive and keep using your unreadeable names.
Yeah using o and v makes no sense. It doesn't tell you anything at all.
v means value - yeah great what kind of value? Is it float a string or an integer? Maybe a boolean or you expect a hex string?
o is even worse. It tells you its an object but what kind of object? Is it a function object a menu item or maybe even a self defined image object?
o and v contain no information whatsoever. Having descriptive names is more important.
And what do you do if the variable can be a callable object or a function name?
Recommends AHK Studio
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: your personal AutoHotkey style guide

21 Sep 2018, 04:37

- To me, the main problem when reading code is to know what is a variable and what isn't. Whether the language is familiar or unfamiliar.
- I can instantly see what is a variable in my code, other people's code takes longer to read/debug. This is particularly true of LaTeX for example.
- (Using a letter prefix means that you can use CamelCase for functions and vCamelCase for a variable name, and I find 'camelCase' for variable names awful, and 'var_name' unfortunate.)
- (To use the word 'unreadable' seems a bit perverse, I was converting some code today and noticed how much more readable it was becoming with the 'v' prefixes and CamelCase.)
- My naming system makes it very easy to identify/enumerate/replace variables (and to not accidentally replace words in comments), and to spot unusual entries in the variables list.
- I don't think people appreciate how quick and easy my systems make it to code.
- In some (many/most?) languages: XXX(), this could be a function or a variable. So you need some kind of identifier. So perhaps a prefix e.g. sXXX, or the use of numbers as a suffix: XXX1. I'm moving to the latter for languages that enforce types since there are just too many types (and thus prefixes).
- I use vFunc and oFunc for function names and function objects(/bound funcs).
- (There's a logic to using oHWnds/oPIDs instead of oHWnd/oPID, but I find it neater without the 's'.)
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
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: your personal AutoHotkey style guide

21 Sep 2018, 05:08

Its more readeable because your used to it. I'm not used to it so its not readeable to me.
Also in other languages like python or javascript there is not a single difference between a variable and a function.
I really assumed you had at least the sense to choose something different than variable as the long version for v.
Everything you put that v in front is a variable. But also everything you don't put this in front of.
Essentially there is no difference between a variable and a function in many languages.
It's pushed the furthest in languages like python or javascript where you cannot have function but only references to function types.
What you say is the most senseless thing I've heard of you since quite a while - you are once again loosing any contact with the actual reality of programming and development.
Thus I will not respond any further - since it would be a massive waste of time.
Recommends AHK Studio
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: your personal AutoHotkey style guide

21 Sep 2018, 05:43

I really assumed you had at least the sense to choose something different than variable as the long version for v.
Everything you put that v in front is a variable. But also everything you don't put this in front of.
- I don't understand this point. Use something other than 'v', like what?
- I use 'v' for non-object variables and nothing else.
What you say is the most senseless thing I've heard of you since quite a while - you are once again loosing any contact with the actual reality of programming and development.
Thus I will not respond any further - since it would be a massive waste of time.
- I don't know what you're referring to. I said that variables and functions can look(/be) similar in some languages, which you agreed with.
- (Let's pretend that I didn't know that. There are all sorts of things that people happen not to know. You could be gentler with people.)
- (You're using words/phrases like 'senseless'/'losing touch with reality'/'waste of time' over literally nothing. This can be bad because you can mislead people reading your comment.)

- Here is my view on variable names:

Code: Select all

InStr(vText, vNeedle, vCaseSen, vPos, vOcc) ;preferred
InStr(text, needle, caseSen, pos, occ)
InStr(text, needle, case_sen, pos, occ)

SplitPath, vPath, vName, vDir, vExt, vNameNoExt, vDrive ;preferred
SplitPath, path, name, dir, ext, nameNoExt, drive
SplitPath, path, name, dir, ext, name_no_ext, drive
- Another advantage of my system is that you can stick in an extra word at the start, without having to change the capitalisation. And that key words always start with a capital (useful when searching).

Code: Select all

vPath -> vPfxPath
path -> pfxPath
path -> pfx_path
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

25 Sep 2018, 14:20

On Stack Overflow, it seems that all the Java for/while examples use curly braces for one-liners even if they're not required e.g.:
java - Regex to find all matches - Stack Overflow
https://stackoverflow.com/questions/328 ... ll-matches

Code: Select all

//with
    while (m.find()) {
        matches.add(m.group(0));
    }

//without
    while (m.find())
        matches.add(m.group(0));
What's more common, use or don't use? Are they using some official/unofficial Java style guide?
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
SL5
Posts: 879
Joined: 12 May 2015, 02:10
Contact:

Re: your personal AutoHotkey style guide

27 Sep 2018, 02:36

jeeswg wrote:On Stack Overflow, it seems that all the Java for/while examples use curly braces for one-liners even if they're not required e.g.:
java - Regex to find all matches - Stack Overflow
https://stackoverflow.com/questions/328 ... ll-matches

Code: Select all

0. while (m.find()) 
    {
        matches.add(m.group(0));
    }
1. while (m.find()) {
        matches.add(m.group(0));
    }
2.    while (m.find())
        matches.add(m.group(0));
What's more common, use or don't use? Are they using some official/unofficial Java style guide?
i dont konw. i always using 2 or 1. 0 i really dont like.
with PHP i often see style 0.

i like this (nearly like 1.) https://de.wikipedia.org/wiki/Einr%C3%B ... small-Stil
User avatar
tank
Posts: 3122
Joined: 28 Sep 2013, 22:15
Location: CarrolltonTX
Contact:

Re: your personal AutoHotkey style guide

27 Sep 2018, 09:53

Code: Select all

;YES
;~ comment
;NO
; comment

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

;YES 
Loop, 3
	{
	MsgBox
	Loop, 3
		{
		InputBox, vInput
		}
	}
;NO (K&R: one true brace)
Loop, 3 {
	MsgBox
	Loop, 3 {
		InputBox, vInput
	}
}

;NO
if (var1 = var2)
;YES
if ( var1 = var2 )

;YES
oArray := ["a", "b", "c"]
;NO
oArray := ["a","b","c"]

;YES
DllCall("MessageBox"
		, Ptr, 0
		, Str, "text"
		, Str, "title"
		, UInt, 0)
;NO
DllCall("MessageBox", "Ptr", 0, "Str", "text", "Str", "title", "UInt", 0)

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

;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

;YES
txtText
arrArray
nNumber
;NO
text
array

;YES
if !(var1 = var2)
;NO
if not (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%

;YES
vText = 
(
line 1
line 2
line 3
)
;NO (the 'n' combines with the string, bad for spellchecking)
vText := "line 1`r`nline 2`r`nline 3"

;YES
vText := A_Space A_Space
;NO (never have multiple consecutive spaces)
vText := "  "

;YES (indentation of 1 tab)
if (var1 = var2)
	MsgBox
;NO (indentation of 4 spaces)
if (var1 = var2)
    MsgBox

;AMBIGUOUS CASES ...

;NEVER
if (var1 = var2)
	MsgBox
;IT'S OK (UNNECESSARY CURLY BRACES)
if (var1 = var2)
{
	MsgBox
}

;IT DEPENDS
var := 1 + 1
;IT DEPENDS
var := 1+1
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: your personal AutoHotkey style guide

22 Mar 2019, 10:29

When dealing with structs, I see two approaches. I find the former far simpler and easier to debug.

Code: Select all

;read UInt at offset 20 (64-bit AHK) or 12 (32-bit AHK):
vNum := NumGet(&vData, A_PtrSize=8?20:12, "UInt") ;yes
vNum := NumGet(&vData, A_PtrSize*2+4, "UInt") ;no
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
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: your personal AutoHotkey style guide

25 Mar 2019, 03:52

Code: Select all

; I prefer
DllCall("ntdll\NtQuerySystemInformation", "int", 0x5, "ptr", &buf, "uint", size, "uint*", 0)

NumGet(&buf + 56 + A_PtrSize * 3, "ptr")

; and / or for special cases with while (addr)
addr := &buf
while (addr) {
	; NumGet(...)
	NumGet(addr + 56 + A_PtrSize * 3, "ptr")
	; more NumGet(...)
	addr += NumGet(addr + 0, "uint")
}
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: your personal AutoHotkey style guide

01 Apr 2019, 15:23

1. De Morgan's Rule - which one?

Code: Select all

if (!w || !h)
    ; Do stuff
or

Code: Select all

if !(w && h)
    ; Do stuff
2. Negation - inside the parenthesis or outside the parenthesis? This has bothered me for the longest time.

Code: Select all

if (!str)
    ; Do stuff
or

Code: Select all

if !(str)
    ; Do stuff
I'm not interested in a argument based on "it looks better". I'm sure if many of you look through your code you'd seem to use both styles in equally and consistently. For example if I were negating a value from an object, I'd tend to write (!obj.value) but if I were negating the object itself, I'd tend to write !obj or !(obj). Would be curious to see what you guys find out after looking at your code!
User avatar
SL5
Posts: 879
Joined: 12 May 2015, 02:10
Contact:

Re: your personal AutoHotkey style guide

01 Apr 2019, 15:32

update: Sorry I did not read the note on "De Morgan's Rule" yesterday

so far I use it anyway the round bracket outside: means 1.1 if (!w || !h) or 1.2 if (!(w && h))
Of course this one is a bit ugly: 1.2 if (!(w && h)).
this almost always has historical reasons (refactoring an old source).
in other languages, I often use then newline:

Code: Select all

if( !(w && h)
  && (a && b)
  && ....
With such a question, I often look for the development in very new programming languages:
https://kotlinlang.org/docs/reference/control-flow.html
Here you can see that the brackets are always outside and exist.

but I find the arguments that count must rather be simplicity and clarity less my habits.
means maybe if w insstead of if (w.

However, the space saving is very low and I would therefore tend to neglect.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: your personal AutoHotkey style guide

01 Apr 2019, 15:40

Great post iseahound. Cheers.

Code: Select all

;De Morgan's laws
if (!w || !h) ;generally yes, usually more logical/intuitive
if !(w && h) ;generally no, might do this on a long list (yes if it was intuitive in the context)

;not equals comparison
if !(a = b) ;yes, more intuitive
if (a != b) ;no

;negation
if !str ;yes
if !(str) ;OK, 2nd best, more readable than (!str)
if (!str) ;OK

;negation and assignment
if !(str := value) ;more readable than the others
if !str := value
if (!str := value)
In C++, if-statements must be in parentheses, so this might skew what I choose for that language.

[6000th post]
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
SL5
Posts: 879
Joined: 12 May 2015, 02:10
Contact:

Re: your personal AutoHotkey style guide

10 Apr 2019, 09:39

SL5 wrote:
01 Apr 2019, 15:32
.. so far I use it anyway the round bracket outside ...
... but I find the arguments that count must rather be simplicity and clarity less my habits.
... However, the space saving is very low and I would therefore tend to neglect.
update 19-04-10_16-36. I found a bit of evolution in my style :P
if !(mode == "DEBUG" && InStr(A_ComputerName,"xyz"))
becouse i dont like:
..."xyz"))) better ..."xyz"))
User avatar
SL5
Posts: 879
Joined: 12 May 2015, 02:10
Contact:

Re: your personal AutoHotkey style guide

20 Apr 2019, 08:04

Pattern "...The least parameters the better". It's both convenient in terms of code clarity and it actually saves stack memory (not much, but every bit is important in the long run).

Having different names helps allot with auto complete as well.

For example i would go for

GetAllSubOrgUnitsList()
GetFirstSubOrgUnitsList()
The reason for this, after get, the first letter that I write will identify what i want...."
Copied from somewhere
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: your personal AutoHotkey style guide

28 Apr 2019, 03:06

I would always return an array with all the values - so most of your naming scheme seems redundant to me.
I would also note down sub with child since it seems like a parent-child relationship.
Lastly functionnames should not use any shortcuts. They are meant to be called by anyone using (potentially even inconsistent) shortcuts only adds confusion.
getChildOrganizationUnits
Recommends AHK Studio
User avatar
aeiklmr
Posts: 17
Joined: 09 Dec 2018, 22:28
Location: Europe
Contact:

Re: your personal AutoHotkey style guide

06 May 2019, 10:35

jeeswg wrote:
12 Apr 2018, 02:19
- Great feedback so far, especially re. Hungarian notation, thanks.
- I don't think that noting the exact type is that important, but distinguishing between variables/objects is useful.
[/code]
This is strange. I am Hungarian and did a very similar thing, but I've never heard about this thing before. :shifty:
I like to think extroverts have attention dependency disorder.

my projects: D2 Reconsidered - Diablo 2 Resurrected script | HideMyIcon, customizable desktop icon hider for Windows 10

Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 10 guests