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.
User avatar
Chunjee
Posts: 330
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: your personal AutoHotkey style guide

17 Oct 2019, 08:44

Switches are great if you have something with 3 or more outcomes. The alternative is large if else stacks that are don't read well after 4 or 5 options. Till recently that is all ahk has had concerning the matter so it's getting a typical responsive from the userbase.

I welcome the switch control flow with open arms. The main drawback I see currently is that uncompiled scripts utilizing it must be paired with a very up to date version of ahk.
DaveT1
Posts: 74
Joined: 07 Oct 2014, 11:23

Re: your personal AutoHotkey style guide

09 Nov 2019, 04:19

I'm loving this thread - learning so much from you all. Style is great and, although I am a live and let live kinda guy, I do feel strongly that consistency and maintainability are over-ridingly important (as others have pointed out). Anyway, that said, I do have a specific question:

Why do most here not seem to like NOT, AND, OR and prefer !, &&, ||? To me, as a very inexperienced programmer, the former are more meaningful. But I see that AHK now recommends using ! instead of NOT. That suggests (being 'self-consistent') that I should generally use !, &&, || - but why?

Thanks for any help.
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: your personal AutoHotkey style guide

10 Nov 2019, 17:00

ERROR HANDLING

I've updated my post re. error handling:
your personal AutoHotkey style guide - Page 5 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=81&t=47140&p=294803#p294803

One interesting existing thread re. error handling:
ErrorLevel - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=37&t=49733

Although, I would recommend starting a separate thread re. error handling, as it's a big topic.
SWITCH STATEMENTS

I basically say all I want to say re. 'switch', here:
One Line If Statements - Page 2 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=13&t=62478&p=276290#p276290

Switch statements are useful for brevity/clarity. Although, if AHK allowed one-line if-statements, or ways to combine multiple lines into single lines, or ways to combine 'break'/'continue' with multi-line (comma-separated) statements, switch statements wouldn't be as useful.

AHK's switch statement differs in some key ways from C++'s switch statement (and other common switch statement implementations). But arguably in a good way. Some differences are listed in the link above.
NOT, AND, OR v. !, &&, ||

I find '!' more readable (and more elegant) than 'NOT'.

'!' and '&&' are shorter than 'NOT' and 'AND', and don't ever require spaces.
This can make complicated formulae/expressions far more readable.

The symbol versions are more common in other programming languages.
Useful if you move between programming languages often.
Although, that doesn't make the symbol versions better per se.

I might misread 'not'/'and'/'or' or 'NOT'/'AND'/'OR', thinking that the bitwise versions were intended.
E.g. capital letters are commonly used when referring to logic gates.
The symbol versions give me certainty when reading/writing the code.
Bitwise-not (~) [looks like a stretched 'n']
Bitwise-and (&)
Bitwise-exclusive-or (^) [XOR] [looks like the bottom of an 'x']
Bitwise-or (|)
That said, an easy typo is to use &/&&, |/|| incorrectly.

When using AHK, I think I started using '!' immediately or almost immediately, because of how clear it is.
I did use 'OR' and 'AND' at some point.
'&&' was easy enough to learn, since '&' (ampersand) means 'and'.
Even if I didn't use '||' for 'or', I'd still need to remember that '|' was bitwise-or.
Once I started using '||', I found it very intuitive to use. And it helped reinforce that '|' is bitwise-or.

So, I found '!' and '&&' intuitive, I needed to know that '|' was a form of 'or' anyway, and at some point understanding '|'/'||' as forms of 'or' become second nature.
So with both the symbol and text versions being instantly understandable to me, and the symbol versions being shorter/more elegant, I generally prefer the symbol versions.

Code: Select all

;==============================

;I find '!' more elegant, and more immediately readable

;if I must use 'NOT', I find it clearer if it's upper case

var := !var
var := not var
var := NOT var

;number to boolean
var := !!var
var := not not var
var := NOT NOT var

;==============================

;I find the symbol versions look better

var := var1 && var2
var := var1 and var2
var := var1 AND var2

var := var1 || var2
var := var1 or var2
var := var1 OR var2

;==============================

;I find the '!' versions far more readable/clear

if !MyFunc(var1) && !MyFunc(var2)

if not MyFunc(var1) && not MyFunc(var2)

if NOT MyFunc(var1) && NOT MyFunc(var2)

;==============================

;since if / || / && are all 2-characters long,
;they stack up and present nicely in a monospaced font

;I prefer 'AND'/'OR' to 'and'/'or',
;however, 'AND'/'OR' jar with the lower-case 'if'

if (var1 && var2)
|| (var3 && var4)

if (var1 || var2)
&& (var3 || var4)

if (var1 and var2)
or (var3 and var4)

if (var1 or var2)
and (var3 or var4)

if (var1 AND var2)
OR (var3 AND var4)

if (var1 OR var2)
AND (var3 OR var4)

;==============================
One thing to note: 'not' has lower precedence than '!'.
I.e. precedence is the same principle as BODMAS/PEMDAS, when considering the order of operations.

Links:
Variables and Expressions - Definition & Usage | AutoHotkey
https://www.autohotkey.com/docs/Variables.htm#Operators
[true v. false]
Excel COM Help - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=33673&p=156354#p156354
GENERAL POINTS

Hello DaveT1.

I am 'live and let live' re. style, consistency is the key.
However, over time I've come to wish that everyone would use Allman style indentation.
My one request to the entire worldwide programming community.
It's just so much more readable.

Fortunately, it turns out that most people agree with me:
Allman style v. K&R style/one true brace style - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=17&t=48809&p=219138#p219138

Btw where does AHK recommend '!' over 'NOT'? Thanks.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
DaveT1
Posts: 74
Joined: 07 Oct 2014, 11:23

Re: your personal AutoHotkey style guide

11 Nov 2019, 03:48

NOT, AND, OR v. !, &&, ||

I find '!' more readable (and more elegant) than 'NOT'.

'!' and '&&' are shorter than 'NOT' and 'AND', and don't ever require spaces.
This can make complicated formulae/expressions far more readable.

The symbol versions...
Goodness, there are clearly untapped (by me!) depths here. I must admitt that the more I look at !, ||, && the more it becomes 'readable'. It also helps distinguish what are clearly AHK language statements from NOT, OR, AND which could be (but obviously arn't) any old text. Think I'm moving inexoriably towards !, ||, && :thumbup:
However, over time I've come to wish that everyone would use Allman style indentation.
My one request to the entire worldwide...
Over the years that I've been dabbling with AHK (as my only programming language), I've been through lots of different 'styles' for braces. I've ended up using the Allman-style as the least-worst compromise between readability (which I very much prize) and compactness (prized, but slightly less so than readability). I even use Allman for single line statements. For short blocks of code, I'd be perfectly happy with indentation dictating associated lines of text (Pascal, Python?), though not sure how this would look for bigger code blocks. But then there would be no code folding! So Allman it is!
Btw where does AHK recommend '!' over 'NOT'? Thanks.
When I wrote this above, I had searched for this to be able to quote where I'd seen it, but couldn't find it. Now you've asked I've re-doubled efforts to find...It's in the Help File | Variables and Expressions | Expression Operators (in descending precedence order), in the table row for "=, ==, <> !=". And I now see that it applies to no longer using <> and using != instead. So that's probably different to what I said about more generally using ! instead of NOT?

Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 3 guests