indentation systems

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

indentation systems

13 Nov 2017, 15:17

I'm looking to collect information about any indentation systems used with AutoHotkey, that are either in some way standard, or commonly used.

I have absolutely no desire to add to my automatic indentation function, but it's possible at some point that I'll add to it.
indent code (automated code indentation) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=37270

Btw the following code is not guaranteed to be a/the standard style, we're just asking the questions.

Is this the most standard style?

Code: Select all

MsgBox ;(indent the auto-execute section?)
return

q::
	; comment ;(I prefer no space after the semicolon)
	MsgBox
	if (a = b)
		MsgBox
	if (a = b)
	{
		MsgBox
		MsgBox
	}
	t := "
	(LTrim
		text
	)"
return

f()
{
	MsgBox
}
Making things a little more complicated, is this standard?

Code: Select all

f()
{
		MsgBox ;(should one tab be removed here?)
	L1:
		MsgBox
	L2:
		MsgBox
	return
	L3:
		MsgBox
}
Some indentation examples:
detect/Remove duplicate values in array - Page 2 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 14#p182214
Anagrams - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 19&t=34240

One of the key rules re. building an algorithm is 'what's allowed in column 1'.
So far I've seen: labels (function/hotkey/subroutines), curly braces, and return/Exit/ExitApp.

They may not look so similar, but here is the equivalence between subroutines and functions. Crucially, subroutines need a return line to mark their end, a return line that would be unnecessary in a function.

Code: Select all

;to demonstrate equivalence:
q::
	MsgBox
return

f() {
	MsgBox
} ;return

;in normal use:
q::
	MsgBox
return

f()
{
	MsgBox
}
Thanks for reading. Please comment.

Links:
Need your opinion with "Indentation" - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=21958
Last edited by jeeswg on 13 Nov 2017, 17:52, edited 1 time in total.
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
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: indentation systems

13 Nov 2017, 15:32

Not sure if this is the kind of information you're looking for, but my indentation system is rather inconsistent. I will indent only when I feel the need. I otherwise try to indent according to flow, particularly when it comes to helping others on the forum. Here are a few code samples of mine and from working with others on the forum:

Code: Select all

^6::
CoordMode, Mouse, Screen 
x := (A_ScreenWidth / 2)
y := (A_ScreenHeight / 2)
mousemove, x, y
sleep 200
Send, {MButton}
MouseMove, 5, 30, 5, R
Return

^7:: ; update color of controls
Loop 3
{
Gui, Add, Text, % (A_Index=1?"Section":"xs") . " vSample" A_Index, █
Gui, Add, Edit, x+10 vEditor%A_Index% gUpdate_Sample
}
Gui, Show
return
Notice here that I don't indent at all. I could indent the Loop, but I feel like because it's such a short snippet, that I don't need to. I think the braces alone keep it visually distinct.

In this one I don't even do an indent even though I probably should have for the inner loop; it doesn't visually pop out so it can be overlooked:

Code: Select all

^5::
Preset1Key1:=11
Preset1Key2:=12
Preset2Key1:=21
Preset2Key2:=22
varP := {Preset1:{Key1:Preset1Key1,Key2:Preset1Key2},Preset2:{Key1:Preset2Key1,Key2:Preset2Key2}}
Loop 2
{
b_Index:=A_Index
Loop 2
MsgBox % "varP[Preset" B_Index "][Key" A_Index "] = " varP["Preset" B_Index]["Key" A_Index]
}
return
In this one, I don't do any indents except for the "Continue" line, which goes with the If above.

Code: Select all

^3::
FileRead, var, 8 Out of 10 Cats Does Countdown - Episode Guide - All 4 - short.htm
pos:=1
While pos
{
pos:=RegExMatch(var,"originalTitle":"\K.*?"",title,pos)
tit:=SubStr(title,1,-6)
pos:=RegExMatch(var,"summary":"\K.*?"",summary,pos)
subpos:=-1, cast:=""
While subpos
{
subpos:=RegExMatch(summary,"[A-Z][a-z]+\W[A-Z][a-z]+",person,subpos+2)
If (person="Jimmy Carr") || (person="Susie Dent") || (person="Rachel Riley") || (person="Dictionary Corner")
   Continue
cast.=person ", "
}
cast:=SubStr(cast,1,-4)
cast:=StrReplace(cast,"`, ","|") ; takes out comma space to leave just normal spaces between first and last name
cast:=StrReplace(cast," ","\") ; replace spaces between first and last name with \
Sort, cast, \ D|
cast:=StrReplace(cast,"|","`, ")
cast:=StrReplace(cast,"\"," ") ; puts back the space between first and last name
SendInput %tit%`t`t%cast%`n
}
return
This code is confusing because the counter++ is indented even though it's not part of the If...

Code: Select all

~LButton::
+LButton::
KeyWait, Shift, D T2 ; times out in 2 seconds
If ErrorLevel ; this becomes true if KeyWait times out
   return ; don't do anything more
   counter++
Tooltip active %counter%
return
Functions I (sometimes) indent apparently:

Code: Select all

func_01(){
	sleep 5000
	SendInput first thread finished
}

Code: Select all

for key, attack in arrayI
	{
;	MsgBox % key "`n" attack "`n`n firstfirst"
	match:=false
	attackI:=attack
;	MsgBox So far
	for jey, attackVI in arrayVI
	{
;	MsgBox % jey "`n" attackVI "`n`n" attackI
		If (attackI=attackVI) || ((attackI="Hi Jump Kick") && (attackVI="High Jump Kick"))
		{
			attackIeasier.push(attackVI "   " arrayVI[attackVI])
;			MsgBox % attackVI "    " arrayVI[attackVI]
		}
	}
;	If match
;	attackIeasier.push(attack "--" match)
	}
This also shows me trying to do multi-level indentation to keep a flow going, but it's so wack and I feel like I don't have any consistency. After a for loop, do I keep the opening brace on the same column as "for" or tab it over? Well, I do both in that code. Then should the commands be on the same column as the opening brace or a column over? Well, I do both in that code. And then I also don't even tab over for the very last command that runs with the If match.

My code is laced with MsgBox diagnostics, which in turn make me require braces (like for the If (attackI=attackVI) || ((attackI="Hi Jump Kick") && (attackVI="High Jump Kick")) part) instead of being able to omit them to do the single line where I push onto an array. But then I just comment out the MsgBox lines as a reminder of what I have already looked at for debugging/diagnosing, instead of removing the MsgBox line and the optional braces.
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: indentation systems

13 Nov 2017, 17:33

Interesting self-critique. :)

Are you trying to indicate that inconsistency is infavourable?
try it and see
...
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: indentation systems

13 Nov 2017, 17:44

I would think in any published code inconsistency would be unfavorable.

In personal code, it just needs to be readily readable. The example of this one, I think is poor form.

Code: Select all

; removed hotkey and variable/array assignments
Loop 2
{
b_Index:=A_Index
Loop 2
MsgBox % "varP[Preset" B_Index "][Key" A_Index "] = " varP["Preset" B_Index]["Key" A_Index]
}
return
I think that if I were to omit braces, I should indent.

Code: Select all

; removed hotkey and variable/array assignments
Loop 2
{
b_Index:=A_Index
Loop 2
    MsgBox % "varP[Preset" B_Index "][Key" A_Index "] = " varP["Preset" B_Index]["Key" A_Index]
}
return
I think it looks slightly better.

And that indent should not be repeated on the next line.

Code: Select all

~LButton::
+LButton::
KeyWait, Shift, D T2 ; times out in 2 seconds
If ErrorLevel ; this becomes true if KeyWait times out
   return ; don't do anything more
   counter++
Tooltip active %counter%
return
Should be, in my opinion:

Code: Select all

~LButton::
+LButton::
KeyWait, Shift, D T2 ; times out in 2 seconds
If ErrorLevel ; this becomes true if KeyWait times out
   return ; don't do anything more
counter++
Tooltip active %counter%
return
I think it makes it much more readable to have a consistent indenting pattern, as it gives a visual cue to what parts of the code correspond to what when we talk about the flow (If/else, Loops).
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: indentation systems

13 Nov 2017, 18:05

jeeswg wrote:Is this the most standard style?

Code: Select all

MsgBox ;(indent the auto-execute section?)
return

q::
	; comment ;(I prefer no space after the semicolon)
	MsgBox
	if (a = b)
		MsgBox
	if (a = b)
	{
		MsgBox
		MsgBox
	}
	t := "
	(LTrim
		text
	)"
return

f()
{
	MsgBox
}
This is pretty much how I indent.

Subroutines would look pretty much the same as a hotkey.

Various loops would look much the same as the if.

Continuation sections where I split a long line will have indents unless it is something where spaces and indenting would matter.

The bigger subjective to me is when do I put spaces and when do I put blank lines to breakup my code.

I tend to be kind of liberal with spaces to make things more readable. I often do stuff like x := y + 7 even though the spaces are not needed I often put them on each side of operators and := but not always.

I tend to put blank lines to break my code up into sections when I kind of finish one thing and start something different.

My style has gotten more fluffy with spaces and blanks over the years.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: indentation systems

14 Nov 2017, 10:32

Exaskryz wrote:I would think in any published code inconsistency would be unfavorable.
Indeed, indentation is a bit like wearing clothes in public, it is not really optional, but you can pretty much go with whichever style you prefer.

Code: Select all

f()
{
		MsgBox ;(should one tab be removed here?)
	L1:
		MsgBox
	L2:
		MsgBox
	return
	L3:
		MsgBox
}
There is an important difference in the above. I'd say that whatever follows L3:, should be indented, because it defines separate subroutine. But not L1: and L2:. So,

Code: Select all

f()
{
	MsgBox ; Yes
	L1:
	MsgBox
	L2:
	MsgBox
	return
	L3:
		MsgBox
	return
	L4:
		tooltip
	return
}
The same goes if you have labels: withing hotkey routines or in other subroutines that start with a label :crazy:

Code: Select all

label37:
q::
	msgbox
	L1:
	msgbox
return
I do not use labels very much, but I am more likely to do it in v2, since functions now have local labels. It can be handy.
For comments I prefer

Code: Select all

; Comment
or
;	Comment
over
;Comment
Cheers.
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: indentation systems

14 Nov 2017, 11:30

A random snip from AHK Studio. No spaces anywhere, very little capitalization, but very strict indentation.

https://www.dropbox.com/s/c8go8h54w1ust ... o.png?dl=0

It is very interesting to observe experts' techniques. Here's a snip from GeekDude's CodeQuickTester. Very strict spacing and indentation, meaningful capitalization, frequent comments.

https://www.dropbox.com/s/8yliobu1hwmdz ... r.png?dl=0
try it and see
...

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww, mmflume and 165 guests