Codestyle: implicit and explicit concatenation

Post a reply


In an effort to prevent automatic submissions, we require that you complete the following challenge.
Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :| :mrgreen: :geek: :ugeek: :arrow: :angel: :clap: :crazy: :eh: :lolno: :problem: :shh: :shifty: :sick: :silent: :think: :thumbup: :thumbdown: :salute: :wave: :wtf: :yawn: :facepalm: :bravo: :dance: :beard: :morebeard: :xmas: :HeHe: :trollface: :cookie: :rainbow: :monkeysee: :monkeysay: :happybday: :headwall: :offtopic: :superhappy: :terms: :beer:
View more smilies

BBCode is ON
[img] is OFF
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Codestyle: implicit and explicit concatenation

Re: Codestyle: implicit and explicit concatenation

Post by swagfag » 17 May 2020, 10:45

i must have been smoking something real good back in 2018, cause looking back, damn nearly none of the scripts i wrote over these past 2 years use explicit concatenation
pretty much in the same boat as @Nextron, use it only to circumvent secret ahk parsing bugs, its just too much noise otherwise

Re: Codestyle: implicit and explicit concatenation

Post by nnnik » 15 May 2020, 11:34

Yeah dont use those Loops. Just use the default loop.

Re: Codestyle: implicit and explicit concatenation

Post by BNOLI » 13 May 2020, 06:12

Nextron wrote:
09 Apr 2018, 12:04
[...] , opening curly braces not on a separate new line.
... that is really something I wanna do, but the Loop command is acting kind' annoying on this matter. There are Loop-flavors that allow a trailing curly brace (function style) at the same line (what I'd prefer) while others won't. Just for the records. Meh :eh:

Re: Codestyle: implicit and explicit concatenation

Post by BNOLI » 13 May 2020, 06:06

... while A A_Space B is even longer than A " " B, isn't it? :shifty:

Re: Codestyle: implicit and explicit concatenation

Post by Helgef » 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.

Re: Codestyle: implicit and explicit concatenation

Post by SKAN » 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

Re: Codestyle: implicit and explicit concatenation

Post by SKAN » 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)

How to concatenate string without spaces in V2

Post by SKAN » 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

Re: Codestyle: implicit and explicit concatenation

Post by jeeswg » 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.]

Re: Codestyle: implicit and explicit concatenation

Post by john_c » 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).

Re: Codestyle: implicit and explicit concatenation

Post by jeeswg » 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"

Re: Codestyle: implicit and explicit concatenation

Post by somhairle » 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.

Re: Codestyle: implicit and explicit concatenation

Post by Nextron » 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.

Re: Codestyle: implicit and explicit concatenation

Post by nnnik » 09 Apr 2018, 12:55

Having space around your code makes it cluttered?
I use lots of space and always use explicit concatenation.

Re: Codestyle: implicit and explicit concatenation

Post by Nextron » 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.

Re: Codestyle: implicit and explicit concatenation

Post by guest3456 » 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

Re: Codestyle: implicit and explicit concatenation

Post by MaxAstro » 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

Re: Codestyle: implicit and explicit concatenation

Post by john_c » 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.

Re: Codestyle: implicit and explicit concatenation

Post by Flipeador » 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..."

Top