indentation systems

Get help with using AutoHotkey and its commands and hotkeys
User avatar
jeeswg
Posts: 3010
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] [Expand] [Download] GeSHi © Codebox Plus



Making things a little more complicated, is this standard?

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Some indentation examples:
detect/Remove duplicate values in array - Page 2 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=39697&p=182214#p182214
Anagrams - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=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] [Expand] [Download] GeSHi © Codebox Plus


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.
User avatar
Exaskryz
Posts: 2690
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] [Expand] [Download] GeSHi © Codebox Plus



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] [Expand] [Download] GeSHi © Codebox Plus



In this one, I don't do any indents except for the "Continue" line, which goes with the If above.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



This code is confusing because the counter++ is indented even though it's not part of the If...

Code: [Select all] [Download] GeSHi © Codebox Plus

~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] [Download] GeSHi © Codebox Plus

func_01(){
sleep 5000
SendInput first thread finished
}


Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



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: 337
Joined: 02 Feb 2016, 17:54

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: 2690
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] [Download] GeSHi © Codebox Plus

; 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] [Download] GeSHi © Codebox Plus

; 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] [Download] GeSHi © Codebox Plus

~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] [Download] GeSHi © Codebox Plus

~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).
FanaticGuru
Posts: 803
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] [Expand] [Download] GeSHi © Codebox Plus


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

Google Search, Dictionary, Thesaurus - Quickly Get Information from Specific Web Resources

[Function] Timer - Create and Manage Timers
Helgef
Posts: 2483
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] [Expand] [Download] GeSHi © Codebox Plus



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,

The same goes if you have labels: withing hotkey routines or in other subroutines that start with a label :crazy:

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] [Download] GeSHi © Codebox Plus

; Comment
or
; Comment
over
;Comment

Cheers.
User avatar
derz00
Posts: 337
Joined: 02 Feb 2016, 17:54

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/c8go8h54w1ustoe/ahkstudio.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/8yliobu1hwmdzrp/codequicktester.png?dl=0
try it and see

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Elgin, Rado_K, SirRFI and 50 guests