• Second (unregistered)

    Third

  • Garreth Q. (unregistered)

    Frist

  • (nodebb)

    Seen in production code, many years ago: if ( some_condition ) (some_other_condition) ? a = b : 0; else some_other_action();

    Too lazy to add braces round the inner if()?

  • (nodebb)

    This is a point where Powershell got it right : $a = if ($condition) { $trueValue } else { $falseValue }

  • (nodebb)

    Most likely just a merge conflict solved incorrectly by including both the before and the after...

  • Andreas (google) in reply to Steve_The_Cynic

    I have problems understanding that snippet...

  • TheCPUWizard (unregistered)
    this.worldUuid = builder== null ? null : builder.worldId;
    

    That would make sense....

    If this was C# rather than C++

    this.worldUuid = builder?.worldId
    
  • bvs23bkv33 (unregistered)

    I bet it is lua

  • (nodebb) in reply to Andreas

    The main statement of the if() is a ternary whose branches are an assignment (true) or just zero (false). Someone could have put braces around a real else-less if(), but I guess that would have been too much work.

    And I would dearly love an explanation of how to put properly-formatted blocks of code in this [REDACTED] forum thing, wossname, No Deb B.

  • Anonymous (unregistered) in reply to Johan_B

    I actually prefer the Python syntax: a if condition else b

  • ShartInMart (unregistered)

    Testing code blocks, will this be code...

  • ShartInMart (unregistered) in reply to Steve_The_Cynic

    From my test, backticks seem to work.

  • isthisunique (unregistered)

    Is that a loose check for null or an exact check?

  • (nodebb)

    I guess that commit message is better than "reticulating splines"

  • Bert (unregistered) in reply to Johan_B

    And every other language family without the (needless, imho) distinction between statements and expressions; Lisp, Scheme, Haskell, *ml, ...

  • Foo AKA Fooo (unregistered)

    Seems like WIP (world in progress) ...

  • Foo AKA Fooo (unregistered) in reply to Anonymous

    I don't think I'll ever get the hang of that (or likewise Perl's "do if" and "do unless"). To me it sounds like a bad manager: "Do this! ... Oh wait, I meant only if (whatever) ..."

    It's not about execution order (I know this can be vastly different from what's written in the source code in many cases), but more about logical order how I think of things. The condition is first, and everything afterwards depends on it. So why would I first want to read one branch that may or may not be taken before I see whether it will be taken?

  • Jerepp (unregistered)

    Have you ever tried to debug somebody's nest trinary statements...

  • Anonymous') OR 1=1; DROP TABLE wtf; -- (unregistered) in reply to TheCPUWizard

    @TheCPUWizard: If the author was checking for builder!=null, that would be quite reasonable, but the author was checking for builder.worldId!=null etc. and will still throw an NPE (or equivalent in whatever language this is) if builder==null.

  • Matt Westwood (unregistered)

    It's a bit rubbish anyway, you've got two different objects with the same stuff in them and you're assigning the properties over one by one. You'd stop and think: refactoring possible here?

  • Appalled (unregistered) in reply to Steve_The_Cynic

    Begin the snippet with (PRE)(CODE) and end it with (/CODE)(/PRE) where "(" is "<" and ")" is ">"

    Pre and Code are HTML tags that will disappear at display time (View Page Source is your friend)

  • Herby (unregistered)

    Yes, one could go back to grandpa (Algol 60) where you didn't have ?:, but you did have 'if', so:

    a := if b then c else d;

    Which was perfectly valid. This appears to be OK in other languages in current use as well as demonstrated by some of the comments.

    Everything old is new again.

  • Scott (unregistered)

    One night I was up late programming, and I wrote something alone these lines.

    if (a < b == true) return true; else return false;

    I realized that I could take out the if statement entirely and just say:

    return a < b == true;

    A few minutes later I realized I must be too tired and went to sleep.

  • ` (unregistered)

    `

  • Brendo (unregistered)

    Python does this pretty well I think. It gets a bit unruly if you start chaining ternary operators together; though I think that's true in all languages

    worldid = builder.worldid or 'some_other_value'

    worldid = builder.worldid if builder else 'some_other_value'

  • Developer Dude (unregistered) in reply to bjolling

    No - I see this all the time in the codebase I work on.

    Comatose devs putting in changes without looking at the code around it, below or after, or the code calling or the code the change calls.

    Lot's of true WTFs in this codebase because enterprise devs don't take the time to read the existing code, much less try to make it better. Over time the cruft just keeps adding up.

    I am a robot - I am a leaf on the wind, watch me soar.

  • Barf 4Eva (unregistered)

    "No - I see this all the time in the codebase I work on."

    "Lot's of true WTFs in this codebase because enterprise devs don't take the time to read the existing code:"

    Wait... aren't you an enterprise dev as well then...? :P

  • Joseph Osako (google) in reply to Steve_The_Cynic

    It's called 'NoseBBleed', silly.

  • (nodebb)

    The == operator might make sense in languages with a === operator, if we'd ignore the second code block.

    Let me guess -- the ternary operator is overloaded with some world details constructors?

  • fred (unregistered) in reply to Bert

    In "other languges" like c .... expression values have no sensible justification. The feature just fell out of the parser as a side effect of the desire to have single-line multiple assignments:

    a=b=c=d=0

    Which, as everyone knows, is a critical language feature.

  • Drone (unregistered)

    This reminds me of some old C++ code that had this pattern everywhere: bool conditional = !!pointer; At first it seems ridiculous, but actually served a purpose: quickly casting a pointer value (or null) to a boolean value in a consistent manner, to be used elsewhere in the codebase (which in some cases relied on particular bits being set for proper type conversion).

  • Christopher (unregistered) in reply to Drone

    This reminds me of some old C++ code that had this pattern everywhere: bool conditional = !!pointer; At first it seems ridiculous, but actually served a purpose: quickly casting a pointer value (or null) to a boolean value in a consistent manner, to be used elsewhere in the codebase (which in some cases relied on particular bits being set for proper type conversion).

    The double-not is not needed. A non-null pointer will convert to a true bool value, and a null pointer will convert to a false bool value, which is about as consistent as one could hope for.

  • Anonymous (unregistered)

    Just for the record, it can get pretty ugly in Python too:

    ', '.join("FizzBuzz" if not i%15 else "Buzz" if not i%5 else "Fizz" if not i%3 else str(i) for i in range(1,101))
  • Adam Spofford (unregistered)

    Original submitter here. Just an FYI - I guess the code dude read the submission a little bit wrong. The second four lines were the original version, the first four lines were the final version. They didn't appear together; it was changed from one to the other.

  • Kevin Jordan (google) in reply to Jerepp

    Especially fun to do with PHP where they have it backwards.

  • Olivier (unregistered) in reply to Foo AKA Fooo

    I do that currently, in Perl, the "do if" thing.

    I find it makes sense in one case at least: "log a message if log is enable" the important thing is to log that messaged, the if part is secondary. I find it more readable this way.

    And eventually, it is consistent with prototyping and debugging: at file there are a lot of messages that are logged, when the code stabilize, the option to enable/disable logging is added, so is the if statement at the end of the line.

  • Foo AKA Fooo (unregistered) in reply to Olivier

    OK, that's one case I can somewhat understand. OTOH, the common "die unless" idiom seems even stranger this way. Dying is not the important thing here, it's the exceptional case.

  • Foo AKA Fooo (unregistered) in reply to Scott

    Well, you did realize. Many programmers (according to sites like this one) apparently never realize it or even don't think there's anything wrong with it.

  • Ashley Sheridan (unregistered) in reply to Johan_B

    Johan_B (nodebb) This is a point where Powershell got it right : $a = if ($condition) { $trueValue } else { $falseValue }

    That's a ternary.

  • Yazeran (unregistered) in reply to Olivier

    Yep. I do the same. It adn also be easier for some other (non-log) statements to be done in this way, but the benefit of that quickly degrade as the do part increases beyond a certain (poorly defined) length of code.

    For instance

    a = get_val(this) unless (defined(a));

    makes sense and is easy to understand whereas

    a = (estremely long line of gibberish----------------------------------------------------------------------------------------) unless (defined(a))

    does not necessarily make sense....

  • Foo AKA Fooo (unregistered) in reply to Yazeran

    I'm already doubtful of your first example. E.g., if I was trying to find out where some unset/default value comes from, I'd read across this line and think "ah, a is assigned here, so I can't be that" ... (Of course, with a single line, you'd notice, but imagine a lot of assignments, some with if/unless, some not.) I find it easier if a line starting with "a = " means there's definitely an assignment of a (if the line is reached, of course; that's why we use indentation to easily see conditionals and loops).

    For a similar reason, I prefer ternaries in a number of cases, i.e. with "a = c ? t : e;", I see immediately that a is assigned something, whereas with "if c then a = c; else a = e;" I have to mentally merge two cases to convince me that that's the case.

  • David Mårtensson (unregistered) in reply to Scott

    I regularly refactor such statements in our code :(

    And new ones keeps getting added.

    Often in the form of return variable > 0 ? true : false; //Or some other condition of similar simplicity.

    I hope I never do understand the reasoning behind writing such statements.

  • (nodebb) in reply to Bert

    ...language family without the...distinction between statements and expressions... *ml...

    Genuinely confused. XML? HTML? VRML? UML? YAML? Markup languages and (modeling languages) don't really have statements or expressions, do they?

  • Foo AKA Fooo (unregistered) in reply to David Mårtensson

    I think the "reasoning" is that some people seem to make a mental difference between Boolean values and comparisons, roughly speaking. So they're like, "variable > 0" can't be a value that you can return, it's just a comparison, so I must make it into a value. Or, reading a bool variable gives a value, so you can't use it as an if-condition, you need a comparison there.

  • Bert (unregistered) in reply to jkshapiro

    Terrible sorry. Imagine I said "programming" there; *ml meaning metalanguage, i.e. OCaml, SML, Alice, etc. Aside: if your language doesn't have statements or expressions, there is no distinction to make, so my point would still stand.

  • Connor Peet (google)

    If this is JavaScript, then the code does make sense (the the extent that JavaScript code makes since)

    In JS null == undefined, they may have wanted to ensure that undefineds are "converted" to nulls so that strict equality could be used to it later.

  • asr (unregistered)

    i have co worker who does (c#) a == true ? true: a all the time. this is the simple one i keep cleaning up code behind him. he does condition ? true : some toehr expression ? true : somethingelse; for every boolean operation.

  • (nodebb) in reply to Christopher

    The one use of the double-not is to suppress a warning generated by (some non-zero number of) compilers that you are doing an implied comparison and so the assignment will not be as fast as you think. The alternative is to write a not-equal-zero test.

  • Ron Fox (google) in reply to Johan_B

    First saw a construct like that in PERL --as has been pointed out, that language feature far pre-dates PowerSnail.

  • ChaoticEd (unregistered)

    I actually ran into a issue where you had to write something simular to get it to work in debug mode. The statment "return basket;" caused a NullReferenceException in debug mode but not in release mode. It turned out that the developer of the e-commerce library used had added a debug assert in an implicit cast oparator which was triggered in debug. As a result the above statement had to be written "return basket == null ? null : basket;". One of the few places where a comment was warranted so noone cleand up the code and thus reintroduced the bug.

Leave a comment on “As The World Ternaries”

Log In or post as a guest

Replying to comment #:

« Return to Article