Sign in to follow this  
Followers 0
MyEarth

New ternary operator - Discussion

50 posts in this topic

There is any advantage to use this:

(1 = 1) ? MsgBox($MB_OK, "Result: 1=1", "True!") : MsgBox($MB_OK, "Result: 1=1", "False!")

Instead of this?

If 1 = 1 Then 
MsgBox($MB_OK, "Result: 1=1", "True!")
Else
MsgBox($MB_OK, "Result: 1=1", "False!")
EndIf

Share this post


Link to post
Share on other sites



 

There is any advantage to use this:

(1 = 1) ? MsgBox($MB_OK, "Result: 1=1", "True!") : MsgBox($MB_OK, "Result: 1=1", "False!")

It's shorter syntax. It makes life easier when say you want to do a conditional in a string or something, without setting variables.

Share this post


Link to post
Share on other sites

 

There is any advantage to use this:

(1 = 1) ? MsgBox($MB_OK, "Result: 1=1", "True!") : MsgBox($MB_OK, "Result: 1=1", "False!")

Instead of this?

If 1 = 1 Then 
MsgBox($MB_OK, "Result: 1=1", "True!")
Else
MsgBox($MB_OK, "Result: 1=1", "False!")
EndIf

in this particular case no but see $tagREBARBANDINFO in StructureConstants.au3

or _Min/_Max in Math.au3

Share this post


Link to post
Share on other sites

It's just a shortcut that a few languages have. It's use depends on the coder. I personally don't use it at all even in C/C++, other coders like to use it everywhere.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Personally i don't like it, maybe is less code but it's not "readable" like an If...EndIf

Edited by MyEarth

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I personally don't use it at all even in C/C++, other coders like to use it everywhere.

I love ternary. When I use them, I very rarely nest them inside of each other as this does get confusing.

Personally i don't like it, maybe is less code but it's not "readable" like an If...EndIf

It's a good job that If..EndIf still exist :) Edited by James

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Just for know, why whe can't have the same logic of ternary but with a if...endif?

If <expression> Then statement1 Else statement2

Maybe not multiple ElseIf but at least one Else on the same line?

Edited by MyEarth

Share this post


Link to post
Share on other sites

Just for know, why whe can't have the same logic of ternary but with a if...endif?

If <expression> Then statement1 Else statement2

Maybe not multiple ElseIf but at least one Else on the same line?

Now I find that confusing.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

I find many a time, the reason I use ternary is to do a condition within a concatenation of strings. In AutoIt:

$bDebug = False
MsgBox(0, "", ($bDebug ? "Oh hai James, you're secretly the most awesome of them all.." : "Yo! You're awesome dude..."))

Try doing that with an If...EndIf - usually you can't.

Edited by James

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Why is confusing? It's the same logic of ternary but write in "autoit style". 1 line < 4 lines, with many ElseIf on the same line can be confusing, with only one Else for me it's a good compromise. Anyway i know will be never added :D

Edit: James, i don't have say ternary is not useful, but in the most of the case you can do the same with If...EndIf and ternary isn't "redeable", at least for a noob like me

Edited by MyEarth

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Just for know, why whe can't have the same logic of ternary but with a if...endif?

If <expression> Then statement1 Else statement2

Maybe not multiple ElseIf but at least one Else on the same line?

 

You can write something like this on one line but it gets more complicated. Instead it's easier to just create a helper function which you can call to return the correct result (1 line of code in the main script), or use this new feature if more suitable.

Edited by czardas

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

I've seen something like that before, but it was "statement1 IF expression ELSE statement2". Think it might be python bit not actually sure.

Generally I use the conditional operator for things like:

 

ConsoleWrite($n & " green bottle" & ($n > 1 ? 's' : '') & @LF)
Edited by Melba23
Removed apology as it is now in the right place!

Share this post


Link to post
Share on other sites

Why is confusing? It's the same logic of ternary but write in "autoit style". 1 line < 4 lines, with many ElseIf on the same line can be confusing, with only one Else for me it's a good compromise. Anyway i know will be never added :D

Edit: James, i don't have say ternary is not useful, but in the most of the case you can do the same with If...EndIf and ternary isn't "redeable", at least for a noob like me

Ternary is confusing when you put them together, look at this:

$bOne   = False
$bTwo   = False
$bThree = False
$bFour  = True

ConsoleWrite(($bOne ? : "$bOne is True" : ($bTwo ? "$bTwo is True" : ($bThree ? "$bThree is True" : ($bFour ? "$bFour is True" : "Oh god, the Mayans were right, the world is going to end!")))) & @CRLF)

Tell me that doesn't get confusing.

Share this post


Link to post
Share on other sites

Ternary is confusing when you put them together, look at this:

$bOne   = False
$bTwo   = False
$bThree = False
$bFour  = True

ConsoleWrite(($bOne ? : "$bOne is True" : ($bTwo ? "$bTwo is True" : ($bThree ? "$bThree is True" : ($bFour ? "$bFour is True" : "Oh god, the Mayans were right, the world is going to end!")))) & @CRLF)

Tell me that doesn't get confusing.

Not confusing at all. Optimizing compilers will just have the single line ConsoleWrite("Oh god, the Mayans were right, the world is going to end!" & @CRLF)

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

Ternary is particularly useful when declaring constants. This goes for any language that has ternary. When dealing with const references even more. For example in AutoIt you would use ternary good if you would do this:

Const $MY_SUPER_MEANINGFUL_CONST = @AutoItX64 ? "Bzz" : "Hrr"
;...

...Try without ternary and see what you get.

Edited by trancexx
3 people like this

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Not confusing at all. Optimizing compilers will just have the single line ConsoleWrite("Oh god, the Mayans were right, the world is going to end!" & @CRLF)

Last time I checked, humans aren't compilers. It's confusing for a human to read that when surrounded by other code, other conditions etc.

Share this post


Link to post
Share on other sites

And the OP is terrible terrible example. Less terrible would be:

MsgBox($MB_OK, "Result: 1=1", (1 = 1) ? "True!" : "False!")

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

Some of you have already seen it, but I find making this post hard to resist, and someone might be interested although the similarity ends with the word ternary.

For another use of the word ternary see >FlipFlapFlop

Edited by czardas

Share this post


Link to post
Share on other sites

And the OP is terrible terrible example. Less terrible would be:

MsgBox($MB_OK, "Result: 1=1", (1 = 1) ? "True!" : "False!")

That example was made by Melba, i have just copy-past from him :D

Bad bad example Melba lol

Ok, ternary is so cool but we have lived without that for years, now we can make new thing that before?

Share this post


Link to post
Share on other sites

Ok, ternary is so cool but we have lived without that for years, now we can make new thing that before?

AutoIt is growing up. Many languages have ternary and since AutoIt is used now for some serious programming, it makes sense to aid the developer.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0