Opened on Jul 7, 2015 at 3:20:22 PM
Closed on Jul 8, 2015 at 8:22:15 PM
Last modified on Jul 9, 2015 at 7:08:53 AM
#3067 closed Feature Request (Rejected)
1 line If...Else Statement
| Reported by: | TheDcoder | Owned by: | |
|---|---|---|---|
| Milestone: | Component: | AutoIt | |
| Version: | Severity: | None | |
| Keywords: | Cc: |
Description
Ability to run a 1 line If...Else statement like this:
If False Then MsgBox(0, 0, 0) Else MsgBox(0, 0, 1)
Attachments (0)
Change History (7)
follow-up: 2 comment:1 by , on Jul 7, 2015 at 6:46:47 PM
follow-up: 4 comment:3 by , on Jul 8, 2015 at 5:07:09 PM
Understood as much but I am not really sure you thought this through.
Which basic like language is using this format of an single If--Else--Endif?
To me the only result is that it makes script less readable, but that's also a matter of taste.
Jos
comment:4 by , on Jul 8, 2015 at 5:54:34 PM
Replying to Jos:
Understood as much but I am not really sure you thought this through.
Which basic like language is using this format of an single If--Else--Endif?
To me the only result is that it makes script less readable, but that's also a matter of taste.
Jos
I have many If...Else...EndIf which only contain 1 line of code in each condition, Also if that If structure is in a already another structure like Switch...EndSwitch it just sucks... As a person who hates spaghetti code, I prefer to use 1 line If statement where possible... Also it makes the code more readable I think, this an example:
; 1 liner If IsValid($sString) Then MsgBox($MB_ICONINFROMATION, "Success", "Done") Else MsgBox($MB_ICONERROR, "Error", "An error has occurred")
; 3 liner
If IsValid($sString) Then
MsgBox($MB_ICONINFROMATION, "Success", "Done")
Else
MsgBox($MB_ICONERROR, "Error", "An error has occurred")
EndIf
The 1 liner is more readable to a normal person than the 3 liner
TD :)
comment:5 by , on Jul 8, 2015 at 8:22:15 PM
| Resolution: | → Rejected |
|---|---|
| Status: | new → closed |
Use Ternary when you need a oneliner.
Jos
comment:6 by , on Jul 9, 2015 at 6:41:44 AM
To add to what Jos said. Don't use ternary for control flow, which is the example you have presented us here. Instead use the ternary operator for something like:
MsgBox($MB_SYSTEMMODAL, '', 'The following string is ' & (IsValid('AEIOU') ? '' : 'not') & ' valid') ; Notice how I have kept the operator scope as small as possible, by only appending 'not' if IsValid() returns false.
The 1 liner is more readable to a normal person than the 3 liner
This is nothing more than speculation, which should be backed up by hard evidence.
As for your request, you don't see developers writing this:
// K&R style, used in Java for example
if (true) { ... } else { ... }
// Usually
if (true) {
...
} else {
...
}
// I prefer Allman, which I use in PHP and C#
If you do, then they have no consideration for others maintaining their code.

Why?
Which problem does this solve?
Jos