1. 29
  1.  

  2. 13

    …nobody tell him about Perl’s syntax.

    1. 2

      Or Ruby’s unless.

      1. 6

        Both of those don’t have else.

        (The correct solution is to make if an expression)

        1. 1

          When I first read this I worried he was railing on if as an expression, but this is more about the loss of readability that comes from tacking if onto expressions.

    2. 9

      The author goes to great lengths to essentially say that he doesn’t like Python’s conditional expressions, but there are a few insights into underlying decisions that are worth reading for language designers.

      1. 6

        To voice a different opinion: I actually like Python’s syntax because I can never remember the syntax of other ternary operators which have pointless characters like ? (this one makes slight sense, but it is just introduced for the ternary operator and serves no other purpose in most languages) and : which makes no sense whatsoever.

        Also, just about all stack based languages have the conditionals the other way round, so first the code blocks and then the if.

        Finally, I even disagree with double negative. In Clojure we have if-not which is quite neat, since when I write code I prefer to have the “early” return value of an expression first, so when reading I can discard the easy case from my mental stack as soon as possible and don’t need to think about it after the “complex” case, therefore I prefer to write code where the “else” branch is the one doing the heavy lifting since it comes last, as I handled the easy cases before already. That does not always make sense, but can be useful.

        I do the same thing in OCaml, really, though for slightly different reasons: in OCaml you need to add parens to nested pattern matches, unless it is the last branch (because of the precedence rules how pattern match branches associate), which is why the code which does the early return (thing | [] -> None) comes first and more complex code potentially involving nested matches comes last.

        1. 7

          To voice a different opinion: I actually like Python’s syntax because I can never remember the syntax of other ternary operators which have pointless characters like ? (this one makes slight sense, but it is just introduced for the ternary operator and serves no other purpose in most languages) and : which makes no sense whatsoever.

          I completely agree with that, but Python’s approach seems weirdly middle-endian. (I am also horribly confused by the order of iteration of [blah for x in xs for y in ys] every time). In Scala I can just write if(cond) a else b and that’s an expression (the same as cond ? a : b would be in other languages), which just seems like the obviously right thing and leaves me amazed how many languages get it wrong.

        2. 3

          Where Erlang uses true as a catch-all, Clojure has the :else keyword. This makes it more readable.

          (cond
            (< n 0) "negative"
            (> n 0) "positive"
            :else "zero"))
          
          1. 16

            :else isn’t anything special in Clojure, it’s just truthy. The following is the same:

            (cond
              (< n 0) "negative"
              (> n 0) "positive"
              true "zero"))
            
            1. 5

              I regularly use :else as the catchall clause in Elixir (same rules as Erlang). Anything truthy will work.

            2. 5

              This guy sounds like a real treat to work with.

              1. 3

                Indeed.