Code Confessions

Status
Not open for further replies.

Apteris

Ars Tribunus Angusticlavius
9,590
Subscriptor
When working on code that doesn't interest me I use nested ternary operators.
I LOLed,

I write malware just sophisticated enough to get by our enterprise security suite, just to mess with my coworkers because I hate them. It randomly plays different noises every 3 minutes. I'm currently working on ode to joy one machine at a time.
I marveled,

I use goto, but only in generated code.
I :scared:.
 

dlp

Ars Tribunus Militum
2,220
neuromaster":g9lsory7 said:
Apteris":g9lsory7 said:
When working on code that doesn't interest me I use nested ternary operators.
I LOLed,
Code:
a = foo ? x :
    bar ? y :
    baz ? z :
          null;
Does that make me a bad person?
If it's meant to be PHP, you're playing russian roulette with the value you get back. So yes, yes it does.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
No. It's more compact and easier to read than a bunch of if/elses. Just make sure you understand precedence rules if bar or baz are compound expressions.

If you're doing a bunch of them *write a switch statement*[1]. If this is annoying for guaranteed assignment checking (i.e. you language doesn't spot that for you) then refactor to a method, with a nice name explaining the meaning of the choice and hey presto, you're done.

About the only reason to keep a ternary form is if you absolutely positively must be an expression (e.g. c# Expression<Func<T>> usage) but even then it's being quite evil so the pay off better be big.

1. assuming your language supports it of course
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
The switch only works if you're comparing one value.

Ah sorry, I made an invalid assumption that all were tests against a single variable.

I still think that would read nicer taking advantage of not needing an else in each case and just having two tests then the (still tail) recursion. I don't think serious terseness is a good thing in and off itself. I have no serious data to back up this belief though.
 

dlp

Ars Tribunus Militum
2,220
neuromaster":1dop8yc1 said:
C. What's it do in PHP?
In short, the operator precedence will make this act differently than you think:

Code:
$res = (1 == 1) ? "Yes" : (2 == 2) ? "Maybe" : "No"; // "Maybe" instead of "Yes"
$res = (1 == 1) ? "Yes" : (2 == 3) ? "Maybe" : "No"; // "Maybe" instead of "Yes"
$res = (1 == 2) ? "Yes" : (2 == 2) ? "Maybe" : "No"; // "Maybe"
$res = (1 == 2) ? "Yes" : (2 == 3) ? "Maybe" : "No"; // "No"

To get it right, you need to wrap each subsequent ternary with a parentheses.
 

Apteris

Ars Tribunus Angusticlavius
9,590
Subscriptor
ShuggyCoUk":2p7ol0sz said:
I use goto, but only in generated code.
I :scared:.

What do you think your compiler is doing?

If you have a nice, provable, AST translation it's fine.
I found it funny for a different reason. Work has me using one of those data integration tools that, as opposed to programming like normal people, has you build programs visually, linking provided components together like a retard playing with Legos. The only thing more horrid than working with it is the code it generates behind the scenes; think 'everything is stored in a global' and 'good luck understanding this loop, mortal'.

So when I read the above, this is where my mind went. Bad tools and bad code are bad.
 

KallDrexx

Ars Tribunus Militum
2,040
dlp":1eku20oe said:
neuromaster":1eku20oe said:
C. What's it do in PHP?
In short, the operator precedence will make this act differently than you think:

Code:
$res = (1 == 1) ? "Yes" : (2 == 2) ? "Maybe" : "No"; // "Maybe" instead of "Yes"
$res = (1 == 1) ? "Yes" : (2 == 3) ? "Maybe" : "No"; // "Maybe" instead of "Yes"
$res = (1 == 2) ? "Yes" : (2 == 2) ? "Maybe" : "No"; // "Maybe"
$res = (1 == 2) ? "Yes" : (2 == 3) ? "Maybe" : "No"; // "No"

To get it right, you need to wrap each subsequent ternary with a parentheses.

Wow, why would the 2nd one be "Maybe"?
 
Status
Not open for further replies.