[Wish] One line while loop (similar to if)

Discuss the future of the AutoHotkey language
User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: [Wish] One line while loop (similar to if)

30 Oct 2014, 04:19

Well if you put it that way... It seems to me that neither is unreadable enough ;)
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
User avatar
hoppfrosch
Posts: 443
Joined: 07 Oct 2013, 04:05
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

Re: [Wish] One line while loop (similar to if)

30 Oct 2014, 08:27

Code: Select all

while wb.Busy, Sleep 10
is definitively more unreadable IMHO - I always have to ponder: Is Sleep 10 an argument of wb.Busy? Or is it the body of the while-loop? :crazy: :D
guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: [Wish] One line while loop (similar to if)

30 Oct 2014, 08:34

fincs wrote:Claiming that

Code: Select all

while wb.Busy, Sleep 10
is less readable than

Code: Select all

while wb.Busy
    Sleep, 10
is IMO extremely ludicrous.
not sure if this is a joke or not

User avatar
fincs
Posts: 527
Joined: 30 Sep 2013, 14:17
Location: Seville, Spain
Contact:

Re: [Wish] One line while loop (similar to if)

30 Oct 2014, 10:38

guest3456 wrote:not sure if this is a joke or not
No, it is not a joke.
hoppfrosch wrote:is definitively more unreadable IMHO - I always have to ponder: Is Sleep 10 an argument of wb.Busy? Or is it the body of the while-loop? :crazy: :D
Not sure if this is a joke or not.

---
I assume people posting in this thread are aware that same-line ifs are already supported, e.g.

Code: Select all

if var = "value", MsgBox it happened
Similar things are supported even in v1.x (!), but only for traditional IFs:

Code: Select all

IfEqual, var, value, MsgBox it happened
fincs
Windows 11 Pro (Version 22H2) | AMD Ryzen 7 3700X with 32 GB of RAM | AutoHotkey v2.0.0 + v1.1.36.02
Get SciTE4AutoHotkey v3.1.0 - [My project list]
just me
Posts: 9423
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: [Wish] One line while loop (similar to if)

30 Oct 2014, 11:31

fincs wrote:I assume people posting in this thread are aware that same-line ifs are already supported, e.g. ...
Yes, I am. But - though not quite sure - I never used this construct, and - quite sure - I never missed it in expressional Ifs.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: [Wish] One line while loop (similar to if)

30 Oct 2014, 14:30

For me saving lines means always to see more code at once and understand/remember/connect more code at once.
Also note that here you can group the code by columns, means you can clearly see straight away what happens for which condition.

For example when you need to reassign different variable or do different action.
Is v1 code here more readable? Really?

Code: Select all

; Ahk v2
     if (ErrorLevel="Timeout")			, b:=var1, TurnOn(a)  ,... , ..., ...
else if (ErrorLevel="NewInput")			, b:=var2, TurnOff(a) ,... , ..., ...
else if (Errorlevel="Max")				, b:=var3, Toggle(a)  ,... , ..., ...
else 									, b:=var4, Switch(a)  ,... , ..., ...

; Ahk v1
if (ErrorLevel="Timeout"){
	b:=var1
	, TurnOn(a)
	, ...
	, ...
	, ...
} else if (ErrorLevel="NewInput"){
	b:=var2
	, TurnOff(a)
	, ...
	, ...
	, ...
} else if (Errorlevel="Max"){
	b:=var3
	, Toggle(a)
	, ...
	, ...
	, ...
} else {
	b:=var4
	, Switch(a)
	, ...
	, ...
	, ...
}
User avatar
hoppfrosch
Posts: 443
Joined: 07 Oct 2013, 04:05
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

Re: [Wish] One line while loop (similar to if)

31 Oct 2014, 01:05

just me wrote: Yes, I am. But - though not quite sure - I never used this construct, and - quite sure - I never missed it in expressional Ifs.
Same here - I even stricty avoid using them ...
fincs wrote:
hoppfrosch wrote:...is definitively more unreadable IMHO - I always have to ponder: Is Sleep 10 an argument of wb.Busy? Or is it the body of the while-loop? :crazy: :D
Not sure if this is a joke or not.
I'm not quite sure myself. I hate this "shortened" syntax in general - it makes reading much more difficult IMHO, because the structure of the code layout does give me no additional information about the code itself. Having the body of the while-loop on new lines and indented shows me on one view the body of the while-loop, without haven't read the contents. Unless I'm a perl-man (which has endless possibilities to produce unreadable code), I try to give code a structure to support my reading behaviour (for eexample consequently using { ...} for bodies, even around one liners and so on).
HotKeyIt wrote:

Code: Select all

; Ahk v2
     if (ErrorLevel="Timeout")			, b:=var1, TurnOn(a)  ,... , ..., ...
else if (ErrorLevel="NewInput")			, b:=var2, TurnOff(a) ,... , ..., ...
else if (Errorlevel="Max")				, b:=var3, Toggle(a)  ,... , ..., ...
else 									, b:=var4, Switch(a)  ,... , ..., ...
}
This comparison is not fair. If each coder would layout code like this, this would be an accebtable way to go. But - honestly - have you ever seen anybody using this code layout? Most coders prefer less typing - that's one of the reasons for the existance of this thread. I don't think many users will use additional spaces to support readability...

What I miss most in AHK - based on this disscusion - is a AHKTidy (code formatter - like toralfs famous Auto-Syntax-Tidy). Having this, everybody could transform the code to his own preferred layout before trying to read and understand it ... ;-)
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: [Wish] One line while loop (similar to if)

31 Oct 2014, 01:47

Why not this way? (with brackets)

Code: Select all

if (ErrorLevel > 9000)    { varA := strA, UberFunc() }
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
fincs
Posts: 527
Joined: 30 Sep 2013, 14:17
Location: Seville, Spain
Contact:

Re: [Wish] One line while loop (similar to if)

31 Oct 2014, 04:19

@hoppfrosch: Readability is subjective. I don't think the language should enforce a way of formatting code. IMO one-line While statements are more readable than having to spread them in two lines in many cases. If you don't want to write code like that, then don't. I fail to see why If should get a special treatment, but not While. Either both or none. (And now that we're at it, For too perhaps).

@jNizM: That is way uglier and cannot be cleanly implemented in the current AHK codebase.
fincs
Windows 11 Pro (Version 22H2) | AMD Ryzen 7 3700X with 32 GB of RAM | AutoHotkey v2.0.0 + v1.1.36.02
Get SciTE4AutoHotkey v3.1.0 - [My project list]
lexikos
Posts: 9552
Joined: 30 Sep 2013, 04:07
Contact:

Re: [Wish] One line while loop (similar to if)

31 Oct 2014, 04:44

I fail to see why If should get a special treatment, but not While.
'If' got special treatment as a substitute for IfEqual etc., which some users still use purely because they allow same-line actions. A lack of this capability, or even the perception that v2 removes some functionality, could be considered a deterrent to switching to v2.
Either both or none.
If it's a matter of consistency or language purity, 'none' is easily the better choice. For instance, Loop couldn't allow it in every case because of ambiguity.
User avatar
hoppfrosch
Posts: 443
Joined: 07 Oct 2013, 04:05
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

Re: [Wish] One line while loop (similar to if)

31 Oct 2014, 05:43

fincs wrote:@hoppfrosch: Readability is subjective. I don't think the language should enforce a way of formatting code. IMO one-line While statements are more readable than having to spread them in two lines in many cases. If you don't want to write code like that, then don't. I fail to see why If should get a special treatment, but not While. Either both or none. (And now that we're at it, For too perhaps).
For sure it's subjective. Asking the question "Either both or none?" I'm rather on the side "None" for the reasons mentioned above.
I probably will not write code like that - the problem for me is, that others might do :-). As I said, the biggest problem is the lack of code reformatter AHKTidy ... (coming in the situation of having to read and analyze code from someone else).

I'm not passionate on the question, whether this feature will be implemented or not. Having a rather long experience in collaborative software development, I think a common coding style gives several advantages in collaboration. If a language has too many syntactic possibilities to write one expression, each variant will be used - with all it's consequences on decreasing readability and therefore complicating collaboration (it tooks much more time to read and understand the code)
User avatar
emmanuel d
Posts: 90
Joined: 17 Nov 2013, 04:45

Re: [Wish] One line while loop (similar to if)

21 Jan 2015, 12:09

hoppfrosch wrote:
HotKeyIt wrote:

Code: Select all

; Ahk v2
     if (ErrorLevel="Timeout")			, b:=var1, TurnOn(a)  ,... , ..., ...
else if (ErrorLevel="NewInput")			, b:=var2, TurnOff(a) ,... , ..., ...
else if (Errorlevel="Max")				, b:=var3, Toggle(a)  ,... , ..., ...
else 									, b:=var4, Switch(a)  ,... , ..., ...
This comparison is not fair. If each coder would layout code like this, this would be an accebtable way to go. But - honestly - have you ever seen anybody using this code layout? Most coders prefer less typing - that's one of the reasons for the existance of this thread. I don't think many users will use additional spaces to support readability...
That is exactly how i would do it, i love this.
You dont use spaces but use tabs. saves typing ;)
And easy to copy, just one line
lexikos
Posts: 9552
Joined: 30 Sep 2013, 04:07
Contact:

Re: [Wish] One line while loop (similar to if)

21 Jan 2015, 17:10

hoppfrosch wrote:Most coders prefer less typing - that's one of the reasons for the existance of this thread.
In an editor with auto-indent, putting the IF's action on a separate line requires less (or equal) typing:
if condition{enter}action
vs
if condition{comma}{space}action ; (space can be omitted, but I think it usually wouldn't be)

Without auto-indent, I suppose you could compare:
if condition{enter}{tab}action
if condition{comma}{space}action

if condition{enter}action
if condition{comma}action

I really doubt that it's about there being less typing, either way.
User avatar
jethrow
Posts: 188
Joined: 30 Sep 2013, 19:52
Location: Iowa

Re: [Wish] One line while loop (similar to if)

03 Feb 2015, 01:42

lexikos wrote:I really doubt that it's about there being less typing, either way.
I agree. I'm gonna go out on a limb & say it's because of the restrictions on how you can write your syntax (not un-similar to complaints against python). I don't want to add another line to my code - I want the simple condition & action on the same line - especially if I'm in a One-Liner type of mood. This becomes extremely important when I'm trying to write beautiful, condensed code - or just trying to impress others with my sophisticated coding skills.

... of course I'm being sarcastic, yet truthful ...

Anyways, I'm gonna jump the gun & say why not take the perl approach and allow:

Code: Select all

action if conditon
; or
action while condition
-_+
Posts: 70
Joined: 06 Dec 2014, 12:43

Re: [Wish] One line while loop (similar to if)

24 Feb 2015, 02:27

How about SleepUntil, (expression), 10?
User avatar
emmanuel d
Posts: 90
Joined: 17 Nov 2013, 04:45

Re: [Wish] One line while loop (similar to if)

26 Feb 2015, 08:26

HotKeyIt wrote:

Code: Select all

; Ahk v2
     if (ErrorLevel="Timeout")			, b:=var1, TurnOn(a)  ,... , ..., ...
else if (ErrorLevel="NewInput")			, b:=var2, TurnOff(a) ,... , ..., ...
else if (Errorlevel="Max")				, b:=var3, Toggle(a)  ,... , ..., ...
else 									, b:=var4, Switch(a)  ,... , ..., ...

; Ahk v1
if (ErrorLevel="Timeout"){
	b:=var1
	, TurnOn(a)
	, ...
	, ...
	, ...
} else if (ErrorLevel="NewInput"){
	b:=var2
	, TurnOff(a)
	, ...
	, ...
	, ...
} else if (Errorlevel="Max"){
	b:=var3
	, Toggle(a)
	, ...
	, ...
	, ...
} else {
	b:=var4
	, Switch(a)
	, ...
	, ...
	, ...
}
Actualy in V1 i do this :lol:

Code: Select all

; Ahk v1
ErrorLevel:="tweet"
  (ErrorLevel="Timeout")  ? ( a:=1, b:="var1")
: (ErrorLevel="NewInput") ? ( a:=2, b:="var2")
: (ErrorLevel="Max")      ? ( a:=3, b:="var3")
: (ErrorLevel)            ? ( a:=4, b:="????") ; a unknown error was given
:                           ( a:=5, b:="NoEr") ; no error was givven
Msgbox,%a%----%b%
How do you like that :thumbup:
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: [Wish] One line while loop (similar to if)

26 Feb 2015, 12:39

Good idea :)
But this is not the problem, though, this thread is about one line While statement :)
While A_Index<5, do something
lexikos
Posts: 9552
Joined: 30 Sep 2013, 04:07
Contact:

Re: [Wish] One line while loop (similar to if)

24 Mar 2015, 22:11

One major problem with if expression, action (current) and while expression, action (proposed) is that comma typically separates parameters or sub-expressions -- not statements/commands. I suppose that either multi-statement comma must support commands (only on the right-hand side), as in x := y, MsgBox, or control-flow statements must not use comma in that way. But even if x := y, MsgBox called the command, (x := y, MsgBox) would not; it would purely be an expression.

Other languages which support same-line actions usually either require parentheses around the If/While condition, or a keyword such as then or do. If (condition) { expression } could be an option since concatenation with an {object: literal} is useless (though not invalid), but that could only work with expressions (due to the ending brace), in which case ternary or && is probably better (though with while, one needs at least an empty block on the next line).
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: [Wish] One line while loop (similar to if)

28 Mar 2015, 15:25

I think the only important statements in if and while are break [, Label] + continue [, Label], otherwise only expressions should be supported.

EDIT:
I think (x := y, break) and (x := y, continue) should throw exception.
lexikos
Posts: 9552
Joined: 30 Sep 2013, 04:07
Contact:

Re: [Wish] One line while loop (similar to if)

12 Jun 2015, 23:40

I have thought about it some more and done some experimentation.

Implementing the following is trivial:

Code: Select all

while condition, statement
for var in expression, statement
for var1, var2 in expression, statement
expression, statement  ; as in:
x(), return y

; even:
if x, y(), else, z()
It is less trivial to intuitively group the statements, as in if x, y(), return z grouping the last two in a block (or likewise when below an if/loop/etc.).

However, I think this is akin to v1 commands allowing expressions in some args, but instead of an arg being either a string or an expression, it's a string, expression or command. Since the change (requiring % for expressions) basically sacrifices convenience for "purity", it seems difficult to justify the one-liner syntax, which does the opposite.

If x(), break was valid, maybe (x(), break) should also be valid. However, that would require more extensive changes to the way the program works. If that isn't implemented in v2.0, the risk of confusion could be mitigated by prohibiting keywords as variable names (and perhaps this should be done anyway, like HotKeyIt suggested), but reserving all command names might be too restrictive, especially since any user-defined function can be used as a command.

Return to “AutoHotkey Development”

Who is online

Users browsing this forum: lexikos and 39 guests