Codestyle: implicit and explicit concatenation

Talk about anything
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Codestyle: implicit and explicit concatenation

09 Apr 2018, 07:35

What would you prefer and why: explicit concatenation or implicit concatenation?

Code: Select all

; Explicit concatenation
msgBox % var . "bar"

; Implicit concatenation
; Less visual noise - good. But there are probably some cases when it's not a good idea to use it?
msgBox % var "bar"
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Codestyle: implicit and explicit concatenation

09 Apr 2018, 08:26

explicit all the time and if it gets to the point of becoming unreadable, extract into a self-documenting variable and/or segment it into several part using the .= operator
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Codestyle: implicit and explicit concatenation

09 Apr 2018, 08:47

@swagfag Thanks. What are your reasons to avoid implicit concatenation?
guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: Codestyle: implicit and explicit concatenation

09 Apr 2018, 08:59

in some cases i will just omit the . but usually i will stick with explicit concat because i think it can be less clear what exactly is happening when you are re-reading the code if you dont. syntax highlighting does help with that though

User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Codestyle: implicit and explicit concatenation

09 Apr 2018, 09:00

I prefer explicit concatenation. It seems to me a good programming practice and brings great clarity to the code.
You must also use it when you split the string into several lines:

Code: Select all

; Explicit concatenation
msgBox % var . "bar"
             . "`n..."
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Codestyle: implicit and explicit concatenation

09 Apr 2018, 09:12

Flipeador wrote:You must also use it when you split the string into several lines
Good point! So, with explicit concatenation the code is consistent everywhere.
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Codestyle: implicit and explicit concatenation

09 Apr 2018, 10:05

I always use explicit concat for the same reason that I always use forced expression mode: It's much more clear what is going on if someone else reads my code. And if I haven't looked at a piece of code in three months, then I qualify as "someone else", too. :P
guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: Codestyle: implicit and explicit concatenation

09 Apr 2018, 10:07

MaxAstro wrote:It's much more clear what is going on if someone else reads my code. And if I haven't looked at a piece of code in three months, then I qualify as "someone else", too. :P
100%

its also why naming variables is so important. using "var" or "x" is lazy and sloppy and very bad practice

User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Codestyle: implicit and explicit concatenation

09 Apr 2018, 12:04

I just did a search in my main script for  .  and in some of my old code I did use it, but for the last couple of years I only use implicit. I don't feel explicit concat has added value except for very specific situation (like spanning multiple lines), but it does make code look cluttered.
Then again, I tend to condense my code as much as possible: nesting function calls, no spaces around comma's and operators, opening curly braces not on a separate new line.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Codestyle: implicit and explicit concatenation

09 Apr 2018, 12:55

Having space around your code makes it cluttered?
I use lots of space and always use explicit concatenation.
Recommends AHK Studio
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Codestyle: implicit and explicit concatenation

09 Apr 2018, 17:21

I think the periods make code look cluttered. Not using much space is just a preference.

Let me illustrate with an extracted line of code I just wrote
Style 1: My original code using implicit concatenation, concentrated to a single line.
Style 2: Same but added explicit concatenation.
Style 3: Dissected line to multiple lines.

Code: Select all

;Style 1
WebRequest.Open("GET","http://" Host "/cm?cmnd=" Command State (user ? "&user=" UriEncode(user) "&password=" UriEncode(pass) : ""),0)

;Style 2
WebRequest.Open( "GET", "http://" . Host . "/cm?cmnd=" . Command . State  . (user ? "&user=" . UriEncode(user) . "&password=" . UriEncode(pass) : "" ), 0 )

;Style 3
Method := "GET"
Uri := "http://" . Host . "/cm?cmnd=" . Command . State 
If ( User ){
	eUser := UriEncode(user)
	Uri := Uri . "&user=" . eUser
	ePass := UriEncode(pass)
	Uri := Uri . "&password=" . ePass
}
WebRequest.Open( Method, Uri )
With style 1 I can naturally read the line, whereas with style 2 the dots require me to focus more on the difference between concatenation and parameter separation. The added characters also come close to wrapping the line, hurting legibility.
Style 3 better separates 'what' I'm doing, but also doesn't read as structured to me as style 1, so I would only use this when more operations are required.
somhairle
Posts: 11
Joined: 09 Apr 2018, 12:05

Re: Codestyle: implicit and explicit concatenation

09 Apr 2018, 23:00

As a newcomer to AHK, I find the use of the . for concatenation odd and a bit confusing. Coming from other languages, I tend to see it as a way to access an object's attributes/properties/methods and not as a way to join strings.

That being said (and completely :offtopic: ), I always prefer explicit over implicit. If one is aware of and used to the syntax, var := a . b is intuitive, where var := a b makes me stop to take in the space.

Personally, I would be more likely to use a library function so I could just
var := join("", a, b) because that's the easiest one for me to take in when perusing old code.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Codestyle: implicit and explicit concatenation

10 Apr 2018, 14:23

- I find that using '.' makes scripts less readable and less clear. E.g. see the original post.
- I feel that code that uses the '.' takes more mental energy to read, and that it wastes horizontal real estate, requiring code to occasionally be pushed down to the next line.
- I've only ever had one problem in one script with omitting the '.', but this was a problem with the AutoHotkey documentation, not omitting the '.'.
possible issue with GetCapacity? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 14&t=32911
- The '.' is useful for defining a string across multiple lines.
- I tend not to mind what syntax people use, as long as they are consistent. Although, I doubt that anyone who tries to always use '.', will always use '.', they will miss it out occasionally.

Code: Select all

;Is the '.' useful? Perhaps not.
MsgBox, % var "text" var var "text" "text" var "text"
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
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Codestyle: implicit and explicit concatenation

11 Apr 2018, 08:03

@jeeswg

Agreed. Personally, I don't use anything unless there is real necessity for it:
  • No {} for single lines after if
  • No . for concat
  • No unnecessary commas, i.e. msgBox % "Text" instead of msgBox, % "Text" or msgBox,,, % "Text"
  • Unquoted numerals, i.e. var := 100 instead of var := "100"
  • No unnecessary () in if-expressions, i.e. if inStr(var, "foo") || inStr(var, "bar") instead of if (inStr(var, "foo") || inStr(var, "bar")) (but here I'm not sure. Probably I will change my mind on this point, because it's too easy to introduce a bug).
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Codestyle: implicit and explicit concatenation

11 Apr 2018, 15:58

I had thought that I might do a (personal) style guide at some point. Well, I've done one now. All are welcome to contribute.
your personal AutoHotkey style guide - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 17&t=47140

• No {} for single lines after if
[Fine. I might do it occasionally.]
• No . for concat
[Fine.]
• No unnecessary commas
[I prefer to use commas, it looks better IMO, and makes it clearer whether the first parameter is/isn't omitted.]

Code: Select all

;initial commas
MouseGetPos, vCurX, vCurY, hWnd, vCtlClassNN
MouseGetPos,,, hWnd, vCtlClassNN
;no initial commas
MouseGetPos vCurX, vCurY, hWnd, vCtlClassNN
MouseGetPos ,, hWnd, vCtlClassNN
• No quoted numerals
[Fine.][Btw sometimes you need to quote numerals e.g. var := "012".][If I intend to use it as a string, I might define it as a string, possibly there would be a speed gain, if you avoid repeated conversion between types.]
• No unnecessary () in if-expressions
[Fine.][I'm not actually 100% sure when you can/can't omit the parentheses.]
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
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

How to concatenate string without spaces in V2

12 May 2020, 04:04

On topic: I prefer explicit concatenation

Is there a way in V2 to concatenate strings without spaces between strings?
in V1 the following works:

Code: Select all

A := "Hello"
B := "World"

C := (A)(A_Space)(B)

MsgBox % C
My Scripts and Functions: V1  V2
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Codestyle: implicit and explicit concatenation

12 May 2020, 04:26

Helgef wrote:
12 May 2020, 04:15
No
Thanks. :)
Any workaround?

The following is long and probably not efficient:

Code: Select all

A := "Hello"
B := "World"

MsgBox Format("{}{}{}",A,A_Space,B)
My Scripts and Functions: V1  V2
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Codestyle: implicit and explicit concatenation

12 May 2020, 04:38

A case where explicit concatenation if hard to read (for me)

Code: Select all

fpath := "C:\Program Files\AutoHotkey\AutoHotkey.exe" ; insert .old before extension

SplitPath, fpath, OutFileName, OutDir, OutExtension, OutNameNoExt
MsgBox % OutDir . "\" . OutNameNoExt . ".old" . "." . OutExtension
MsgBox % Format("{}\{}.{}.{}",OutDir,OutNameNoExt,"old",OutExtension) ; work around
My Scripts and Functions: V1  V2
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Codestyle: implicit and explicit concatenation

12 May 2020, 05:11

On topic: I prefer explicit concatenation
metoo.
Any workaround
You can omit the dots :). (A)(A_Space)(B) looks very odd to me, I think it is also harder to type than A . A_Space. B and longer than A A_Space B.

Cheers.

Return to “Off-topic Discussion”

Who is online

Users browsing this forum: No registered users and 53 guests