in reply to Re: nested tabular ternary
in thread nested tabular ternary

Update: - fixed screwed-up formatting within code tags.

Just an observation regards style. (And please don't get me wrong - this is just an observation, not a critisism - each to their own :)

I find it interesting that both yours and Liverpole's answer use a similar layout style for the ternary. That is - to break the line after an operator.

This of course goes against what is recommended by TheDamian in PBP (pp. 27-29 & 121-123). Personally, I prefer his recommended style, and I would have written your second example like so:

$result = ($major_length == 4800) ? 18 : ($major_length == 8552) ? ($val == 4 ? 33 : $val == 1 ? 18 : die "not handled") : die "not handled";
To me, this is more readable - it looks more like a "table", and it's more obvious that there is some nesting. Also, having an operator at the start of a line makes it much more obvious that it's a continuation of the previous line.

Cheers,
Darren :)

Replies are listed 'Best First'.
Re^3: nested tabular ternary
by diotalevi (Canon) on Apr 11, 2006 at 02:09 UTC

    I don't see the "table" and it looks like you just scattered your code all across the page, as if it were scrambled.

    No wait.. it looks like a bad translation of the original lisp into formatted perl. It really does look like lisp indenting conventions were attempting to be followed in your example.

    (if (= major-length 4800) 18 (if (= major-length 8552) (if (= val 4) 33 (if (= val 1) 18 (error "not handled"))) (error "not handled")))

    Here's a nicer lisp version of the same thing. It's got more in common with what I originally suggested than does what you alleged TheDamian recommended.

    (cond ((= major-length 4800) 18) ((= major-length 8552) (cond ((= val 4) 33) ((= val 1) 18) (error "not handled"))) (error "not handled"))

    The above is just a more verbose version of the following. This is getting to be seriously terse to the point that there's almost not enough whitespace in there. I'm just following standard indenting rules so this is just stock lisp.

    (ecase major-length (4800 18) (8552 (ecase var (4 33) (1 18))))

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      I don't see the "table" and it looks like you just scattered your code all across the page, as if it were scrambled.
      hmm... maybe that has something to do with how it renders in your browser vs. mine, although as it's enclosed in code tags I wouldn't have thought so. But anyway, the idea is that the ":" and "?" parts of the ternary line up vertically - producing the table effect - hence the term "tabular ternary". Have you seen the examples in PBP that I referred to?
      Here's a nicer lisp version of the same thing.
      I've never used lisp, so I can't comment on that :)
      It's got more in common with what I originally suggested than does what you alleged TheDamian recommended.
      um, I'm not sure that I alleged anything. All I did was refer to two of the practices that he recommends in his book PBP, namely:
      • "When producing a value, use tabular ternaries" (pp. 121-123), and
      • "Break long expressions before an operator" (pp. 27-29)

      But anyway, as I said - my comments weren't meant as a critisism, but rather a commentary on style.

      Cheers,
      Darren :)

        I copied and pasted your code, making sure that I'd copied over spaces instead of tabs. Nothing in your code lines up. I'm using Firefox on Linux, something I assume is pretty standard. If you meant something that was organized, what you posted wasn't it. Maybe you had tabs on your end that didn't get conveyed to perlmonks.org

        $result = ($major_length == 4800) ? 18 : ($major_length == 8552) ? ($val == 4 ? 33 : $val == 1 + ? 18 : die "not hand +led") : die "not handled";

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re^3: nested tabular ternary
by ikegami (Patriarch) on Apr 11, 2006 at 02:22 UTC

    I can't make heads or tail of your "more readable" "table". Did you mistakenly used tabs?

    Aligning ? and : provides the best results for me. Parens provide a visual scope:

    $result = ($major_length == 4800 ? 18 : ($major_length == 8552 ? ($val == 4 ? 33 : ($val == 1 ? 18 : die "not handled") ) : die "not handled" ) );

    And then there's Perlish:

    ; $result = ($major_length == 4800 ? 18 : ($major_length == 8552 ? ($val == + 4 ? 33 + : ($val == 1 ? 18 + : die "not handled") ) : die "not + handled" ) )

    None of these are particularly readable &mdash or maintainable. if should be used here.

      I can't make heads or tail of your "more readable" "table". Did you mistakenly used tabs?
      No, I never use tabs (I have vim configured to convert them to spaces), and I have no idea what happened there. It looked fine in Firefox (my default browser) when I first previewed and created it, but after yours and diotalevi's comments, I opened an IE window and took a look, and... ewww, I see what you mean. Anyway, I re-pasted (changed nothing) and now it looks okay for me in IE. So hopefully it makes sense to everyone else now :)
        I use Firefox (1.5.0.1 on WinXP), and your update fixed it for me as well. It's more readable, but it's still a far cry from being clear. It's not obvious which : associates with which condition and which :.