[v2]: Put the most frequently used function parameters ahead of the rest

Propose new features and changes
User avatar
mikeyww
Posts: 27198
Joined: 09 Sep 2014, 18:38

[v2]: Put the most frequently used function parameters ahead of the rest

30 Dec 2022, 21:42

v2:

Some functions have "essential" parameters and "less essential" parameters. For example, a MsgBox always has a message, and sometimes has a title or options. Therefore, a decision was made for the MsgBox syntax to look like this.

MsgBox([Text, Title, Options])

Now consider the case of a Gui text control. It always has text (or a plan to have text), but options are often absent. Nonetheless, the syntax is "backwards", as follows.

MyGui.AddText("Options", "Text")

The suggestion here is to swap the options with the main content-- text in this case-- and to do so for all other functions with this same issue. This would make the essential parameter the first parameter.
just me
Posts: 9530
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: [v2]: Put the most frequently used function parameters ahead of the rest

31 Dec 2022, 04:59

I think you're too late, don't you? AHK v2 has been developed over many years, time enough to suggest breaking changes. It might be something for v3.
neogna2
Posts: 598
Joined: 15 Sep 2016, 15:44

Re: [v2]: Put the most frequently used function parameters ahead of the rest

31 Dec 2022, 06:09

mikeyww wrote:
30 Dec 2022, 21:42
make the essential parameter the first parameter
Good goal but in the case of AddText I think there are stronger reasons for keeping the current syntax, even setting aside backward compatibility.
The Options, Text order is consistent for controls creatable with Gui Object method Add
MyGui.Add(ControlType , Options, Text)
MyGui.AddControlType(Options, Text)

For many of those controls adding options data is common so it ok that that option comes first there. And a consistent pattern for all of them is easier to remember and document.

Another benefit with options first is that position data can line up nicely visually in the editor when creating controls. I find that helpful when trying out different positionings for controls. Compare:

Code: Select all

G := Gui()
G.Add("Text", "x30 y30 w100", "hello world")
G.Add("Text", "xp y+10", "a long text here about how mikeyww provides awesome help to people in the AutoHotkey forums")
G.Add("Edit", "xp y+10")

Code: Select all

; not working code, illustrates larger visual distance for position data if options was 2nd argument
G := Gui()
G.Add("Text", "hello world", "x30 y30 w100")
G.Add("Text", "a long text here about how mikeyww provides awesome help to people in the AutoHotkey forums",  "xp y+10")
G.Add("Edit", ,  "xp y+10")
(edited) I almost always add options even to Text controls, e.g. a name vMyControlName. Because if we create a Gui inside a function we can then pass (or globalize) only the Gui object into other functions yet in those functions still easily change controls like so G["MyControlName"].Value := "new text" (Yes, even without a name we can target controls by ClassNN but in Guis with lots of controls that can get messy.)
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: [v2]: Put the most frequently used function parameters ahead of the rest

31 Dec 2022, 07:37

mikeyww wrote:
30 Dec 2022, 21:42
It always has text (or a plan to have text), but options are often absent
im not so sure about that. parsing some oldforum archive dumps yielded ~5000 Gui, Add, ControlType,s of which, even accounting for v1 crap like v...., g... and hwnd...., ~80 contained no options, whereas ~1000 contained no text. 2% vs 20%
anyway, u can change the order to whatever u like in ur scripts

Code: Select all

#Requires AutoHotkey v2.0.0

MakeGuiGreatAgain()
MakeGuiGreatAgain() {
	for propName in Gui.Prototype.OwnProps()
		if (propName ~= 'Add\w+')
			Switch132(propName, Gui.Prototype.GetMethod(propName))

	Switch132(propName, Method123) => Gui.Prototype.DefineProp(propName, {
		Call: (this, Text?, Options?) => Method123(this, Options?, Text?)
	})
}
User avatar
mikeyww
Posts: 27198
Joined: 09 Sep 2014, 18:38

Re: [v2]: Put the most frequently used function parameters ahead of the rest

31 Dec 2022, 07:44

OK. Thank you (x3) for the details. You all convinced me not to place my AHK order for the year 2030 (v3), and instead use v2 like there is no tomorrow. :)

I hereby disable my gui1.AddText(, wish). Good explanations, really!
User avatar
mikeyww
Posts: 27198
Joined: 09 Sep 2014, 18:38

Re: [v2]: Put the most frequently used function parameters ahead of the rest

31 Dec 2022, 21:24

Thank you; I had not seen that post earlier. Perhaps we can at least agree that (1) Gui syntax is inconsistent with MsgBox syntax in v2-- but that seems OK since, unlike Gui, every MsgBox function must have a message-- and (2) there is no consensus about which approach is better for Gui parameter order. TextOrOptions seemed appealing to me earlier as well-- with MsgBox a noted example of TextOrOptions already present in v1, though this approach might not translate so well to Gui, and putting the options in an object just seems like extra work. I guess I'm mostly convinced that putting the options first has some advantages.

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 16 guests