• 14 (unregistered)

    frist ? "frist" : "first"

  • Pjrz (unregistered)

    At least the 2nd example was indented and formatted reasonably so it wasn't completely unreadable.

    Remy: Actually, I did that part, because otherwise it was all on one line and impossible to see.

  • bvs23bkv33 (unregistered)

    Tern? Ermahgerd!

  • Little Bobby Tables (unregistered)

    I'm prepared to suggest that the first one used to return more than just a number, but also the text description, either "account" or "accounts" according to singular or plural -- then it got refactored to add the legend somewhere else. Just a bit of leftover cruft, easily overlooked, easily fixed. No biggie.

  • Herr Otto Flick (unregistered)

    Without specifying the language, the first example could be perfectly cromulent*. Several languages use "==" to test "is-roughly-equivalent-to-if-you-cast-it-to-this-type" and "===" to test "is-exactly-this-type-and-value". Other languages provide the ability to override the == operator to provide the same functionality. So it could be saying "if accounts is equivalent to 1 when you cast it to an int, change accounts to be an int of value 1, otherwise keep it as it is".

    • But shitty to read.
  • Herr Otto Flick (unregistered)

    PS: Why does the second one have a link to your github in the middle of the code :þ

  • Victor (unregistered)

    Did a little self-promotion slip into the 2nd example? self.buttohttps://github.com/RemyPorter/MassnCog Looks like it should be self.buttonCog without the unrelated github link

  • Sally Flynn (unregistered)

    Even though I'm a serial abuser of ternaries (much to the vexation of one of my friends), that second example causes me pain.

  • (nodebb)

    The worst one I ever ran across was blatant "dangling-else" avoidance.

    if ( some_condition )
       (some_other_condition) ? variable = expression : 0;
    else
       something_else;
    

    Somebody didn't want to write braces around the inner if, so he came up with that monstrosity.

  • (nodebb)

    @Remy: Did you mean the code sample to include “https://github.com/RemyPorter/Mass”? That looks like a bad paste…

  • (nodebb) in reply to Herr Otto Flick

    For your WTF viewing pleasure. Or wait, maybe, rather, against it. Also this way Remy appears to be advertising his toys :D

  • Simon Clarkstone (unregistered)

    Aside from the weird URL stuck in there, Remy put the line-breaks in an odd place. I find it clearer to have:

    variable =
        cond1 ? result1
      : cond2 ? result2
      : cond3 ? result3
      : result4
    
  • (nodebb)

    Remy obviously has autocorrect set up in his word processor so he doesn't have to type out "github.com/RemyPorter/Mass"--he can use the shorter phrase "self.butto" instead. The real question is why the other instances of that string weren't also replaced.

  • (nodebb) in reply to Herr Otto Flick

    To make the second one, the one without the link, simpler, of course.

  • D-Coder (unregistered)

    This one should have unicorns. Aren't we doing unicorns any more?

  • schpeelah (unregistered)

    Example 1 reminds me of one time I found "if A.equals(B) return A; else return B;" - A and B being Java Strings.

  • Dolan Grump (unregistered)

    not frist

  • Anonymous') OR 1=1; DROP TABLE wtf; -- (unregistered)

    Fun fact: URLs can be inserted verbatim into C/C++/Objective-C code, without needing a comment or string literal (limit 1 per function), and the code still compiles. The colon creates a label, and everything after the // becomes a comment.

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

    This is truly a gem in my day. I rather like trivia and, frankly, though this was staring me in the face, I never thought that URLs could be embedded in this fashion. Of course, one may only paste such a URL once per function, but still ...

  • RichP (unregistered) in reply to D-Coder

    The unicorns seem to show up late to the article some days. Bummer, cause I like to play Unicorn golf: Count the number of clicks it takes me to find the unicorns. After 8, I go to the source.

  • I dunno LOL ¯\(°_o)/¯ (unregistered) in reply to Anonymous') OR 1=1; DROP TABLE wtf; --

    Damn, I'll have to use that one someday.

  • Harry (unregistered) in reply to dkf

    Maybe Remy wrote it himself?

  • Some Guesty (unregistered)

    isn't too bad if formatted properly

    buttonItems = 
        negSpacer ?
            (self.ButtonCog ? @[negSpacer,self.buttonCog] : @[negSpacer]) :
            (self.ButtonCog ? @[self.ButtonCog] : @[]);
    
  • ckg (unregistered)

    So I've seen the practice of using nested ternaries to code a case-statement like expression in languages that don't support those. It actually is fairly readable if formatted correctly:

    $result = is_foo($thing) ? "foo" :
              is_bar($thing) ? "bar" :
              is_baz($thing) ? "baz" :
              is_42($thing)  ? "meaning of life" :
                               "default value";
    
  • spadgas (unregistered)

    @Some Guesty - it's not too bad to read. It's still terrible.

  • Works as coded (unregistered) in reply to D-Coder

    My standard CSS for every article exposes the unicorns for me:

    span[onclick] {
    	background: #ded;
    }
    
  • Works as coded (unregistered) in reply to D-Coder

    My standard CSS for every article exposes the unicorns for me:

    span[onclick] {
    	background: #ded;
    }
    
  • Duke of New York (unregistered) in reply to Some Guesty

    It's still bad. The effect is: "Create a mutable arrat that contains the negSpacer if it exists, followed by self.ButtonCog if it exists". It would be so trivial to make the code follow that description that I would only bore people by writing it out.

  • Duke of New York (unregistered) in reply to Duke of New York

    (so boring, in fact, that I didn't notice Remy had done it already. oh well)

  • Guest (unregistered)

    buttonItems = [negSpacer, self.buttonCog].filter(x => x)

  • MiserableOldGit (unregistered)

    Can't hundred percent kick the coder on this one without knowing what was going on. I agree with others this likely started out as a sensible little if statement and got bloated out by new requirements. I've ended up with things like that where the department has a fanatical obsession to the merest whiff of refactoring existing code, and refactoring (in some PHB eyes) includes adding white space and indentation to make stuff readable. Rearranging if statements so they make more sense (or converting them to a switch or vice versa) requires the filling out of a dozen forms, a goat to be slaughtered over them, and a sign off from God.

  • I Saw a Robot (unregistered) in reply to Duke of New York

    Actually it looks to me like it assigns an empty mutable array, then drops that to construct a new mutable array from the contents of one of its immutable arrays. In other words, the first initialization was completely unnecessary.

Leave a comment on “Tern This Statement Around and Go Home”

Log In or post as a guest

Replying to comment #:

« Return to Article