Jump to content

New ternary operator - Discussion


 Share

Recommended Posts

 

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

Link to comment
Share on other sites

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
Link to comment
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.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_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: 22/04/2018

Link to comment
Share on other sites

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
Link to comment
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

Edited by MyEarth
Link to comment
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?

 

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
Link to comment
Share on other sites

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!
Link to comment
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.

Link to comment
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)

Link to comment
Share on other sites

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

♡♡♡

.

eMyvnE

Link to comment
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?

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...