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

Discuss the future of the AutoHotkey language
User avatar
joedf
Posts: 6240
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
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 ;)
User avatar
hoppfrosch
Posts: 314
Joined: 07 Oct 2013, 04:05
GitHub: hoppfrosch
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

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

30 Oct 2014, 08:27

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

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: 2302
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] [Download] GeSHi © Codebox Plus

while wb.Busy, Sleep 10

is less readable than

is IMO extremely ludicrous.


not sure if this is a joke or not

User avatar
fincs
Posts: 500
Joined: 30 Sep 2013, 14:17
GitHub: fincs
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] [Download] GeSHi © Codebox Plus

if var = "value", MsgBox it happened

Similar things are supported even in v1.x (!), but only for traditional IFs:

Code: [Select all] [Download] GeSHi © Codebox Plus

IfEqual, var, value, MsgBox it happened
fincs
Windows 10 x64 Build 14393 / AutoHotkey v1.1.24.04
Get SciTE4AutoHotkey v3.0.06.01 - [My project list]
just me
Posts: 5335
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: 1639
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] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus

User avatar
hoppfrosch
Posts: 314
Joined: 07 Oct 2013, 04:05
GitHub: hoppfrosch
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] [Download] (Script.ahk)GeSHi © Codebox Plus

; 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: 2317
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

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

31 Oct 2014, 01:47

Why not this way? (with brackets)

Code: [Select all] [Download] GeSHi © Codebox Plus

if (ErrorLevel > 9000)    { varA := strA, UberFunc() }
[AHK] 1.1.28.02 x64 Unicode | [WIN] 10 Pro (Version 1803) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
fincs
Posts: 500
Joined: 30 Sep 2013, 14:17
GitHub: fincs
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 10 x64 Build 14393 / AutoHotkey v1.1.24.04
Get SciTE4AutoHotkey v3.0.06.01 - [My project list]
lexikos
Posts: 5897
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

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: 314
Joined: 07 Oct 2013, 04:05
GitHub: hoppfrosch
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)
emmanuel d
Posts: 20
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] [Download] (Script.ahk)GeSHi © Codebox Plus

; 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: 5897
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

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: 180
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] [Download] (Script.ahk)GeSHi © Codebox Plus

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?
emmanuel d
Posts: 20
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] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus


Actualy in V1 i do this :lol:

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

; 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: 1639
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: 5897
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

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: 1639
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: 5897
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

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] [Download] GeSHi © Codebox Plus

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 v2 Development”

Who is online

Users browsing this forum: No registered users and 3 guests