Modify

#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)

comment:1 by Jos, on Jul 7, 2015 at 6:46:47 PM

Why?
Which problem does this solve?

Jos

in reply to:  1 comment:2 by TheDcoder, on Jul 8, 2015 at 8:45:11 AM

Replying to Jos:

Why?
Which problem does this solve?

Jos

Feature Request :)

comment:3 by Jos, 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

in reply to:  3 comment:4 by TheDcoder, 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 Jos, on Jul 8, 2015 at 8:22:15 PM

Resolution: Rejected
Status: newclosed

Use Ternary when you need a oneliner.

Jos

comment:6 by guinness, 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.

comment:7 by TheDcoder, on Jul 9, 2015 at 7:08:53 AM

I understand, thanks for taking the time :)

Modify Ticket

Action
as closed The ticket will remain with no owner.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.