Jump to content

New ternary operator - Discussion


 Share

Recommended Posts

Referring to >this post:

$Export_File = (StringRight(StringLower($Export_File), 4) == ".dma") ? $Export_File : $Export_File & ".dma"

How do you suppose that is returning true?  I suspect you aren't fully grasping how the ternary operator, or AutoIt's interpreter, works. The condition is tested, if its TRUE then what comes between '? .. :' is evaluated, otherwise what follows ':' is evaluated.  After evaluation, if there is an assignment on the left hand side of the ternary expression, the result of the evaluated expression is then moved into that .

Optimizing compilers (which AutoIt isn't) could possibly fix that flawed code by doing a no-op or an append, but its better to fix the problem where it starts - at the programmer level.

 

My understanding of ternary is just fine, it just seems that the LParam is not set. In PHP and JavaScript you can do this:

$bStartString   = "James";
$sEndString     = "Brooks";
$bAppendSurname = TRUE;
 
$sFinalString = $bStartString . ($bAppendSurname ? " " . $bEndString : "") . ",";
 
echo "Hello " . $sFinalString;

Edit: Updated actual, decent example of LAssign with ternary.

Edited by James
Link to comment
Share on other sites

 

My understanding of ternary is just fine, it just seems that the LParam is not set. In PHP and JavaScript you can do this:

$bStartString   = "James";
$bEndString     = "Brooks";
$bAppendSurname = TRUE;
 
$bEndString = $bStartString . ($bAppendSurname ? " " . $bEndString : "") . ",";
 
echo "Hello " . $bEndString;

 

If your understanding of ternary is just fine, then your fingers must be acting on their own to do crazy things like assign a value to itself.  But really, the problem has nothing to do with LValue-ness, but rather poor coding.

It seems you want so desperately to use ternary operators that you're coming up with poor examples.  That PHP code is also bad form (overwriting last name?), and pretty unreadable  Try:

$sStartString   = "James";
$sEndString     = "Brooks";
$bAppendSurname = TRUE;

$sName = $sStartString;

if ($bAppendSurname)
    $sName .= " " . $sEndString;

echo "Hello " . $sName  . ",";
Link to comment
Share on other sites

I was at work and very quickly wrote an example, I wasn't meant to overwrite the $bEndString variable, you can see I was rushing since I prefix a string variable with $b it should've been $s like $sStartString was. I've updated the example.

Questioning my ability to write code is a bit off topic, if you have an issue with me or my code, please PM me.

On topic again, the fact still remains that AutoIt doesn't support left-assign with ternaries.

Edit: I never finished...

Edited by James
Link to comment
Share on other sites

James, I have no problem with you.  You seem like a decent enough person.

I was discussing the bad code being used as examples.  I don't see why you think it's off topic, as it had to do with someone not fully understanding, or at least not showing their understanding in a satisfactory way, of the way ternary operators work. 

In other words, the discussion could help others understand where not to use ternary operators, which is why its relevant.

Looking at your revised PHP code now, the changes you made to create a 'decent example' is still missing the mark in two areas.

  1. In the False case, a "" or non-assignment is done. The primary reason to use ternary operators is to give one of two results or expressions with some meaning to them.
  2. The code lacks readability.  If a one-line statement can't be understood without rereading it a few times, it shouldn't be written.
Link to comment
Share on other sites

If I produce a bad example, I'd like it if others tell me about it. Some people might take offence at having their code scrutinized by others, however I believe it's generally a fruitful and rewarding process in the long term.

On a different note:

Anyone can make mistakes, also in English - and especially if you learn English online. I sometimes wonder why some people express themselves the way they do. Recently an Italian user said shite (which to me is Scouse slang) when he meant to say SciTE. It was just a typo, but I'm sure that the user was unaware of how insulting it came across even after I had pointed out the mistake.

Edited by czardas
Link to comment
Share on other sites

If I produce a bad example, I'd like it if others tell me about it. Some people might take offence at having their code scrutinized by others, however I believe it's generally a fruitful and rewarding process in the long term.

I'm not taking umbrage, I'm just trying to come up with an example that was meant to portray the problem.

@Ascend4nt, whether it's correct in your head or not, it's a fully working example.

@trancexx You should be able to get the result of a ternary expression and then perform another expression on it.

Link to comment
Share on other sites

@trancexx You should be able to get the result of a ternary expression and then perform another expression on it.

Hm, and you can't?

I don't see how your PHP example written in AutoIt misbehaves.

Do you get anything unexpected when your run this:

$bStartString = "James"
$sEndString = "Brooks"
$bAppendSurname = True

$sFinalString = $bStartString & ($bAppendSurname ? " " & $sEndString : "") & ","

ConsoleWrite("Hello " & $sFinalString & @CRLF)

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

I'm not taking umbrage, I'm just trying to come up with an example that was meant to portray the problem.

 

Thank you James, I find the discussion quite useful. Although I haven't used the conditional operator, it seems straight forward enough.

>This example by Mat is very informative:

Edited by czardas
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...