Hacker News new | ask | show | jobs
by dupdrop 353 days ago
Sorry for the petty comment, but if you design your language syntax with `x if cond else y` with the condition in the middle like in the Python if-expression syntax, I cannot trust your judgment and we cannot be friends. (from one of the images of code)

I will take an actual look into it later though, seems interesting.

2 comments

Hey, I'm one of the dev's; We thought about this ALOT like 4 hour long arguments, we asked a lot of our friends and its was really mixed, some people hated the python style ternary, others loved it, we weren't sure what to choose but the numbers were 38.5% for `<expr> if <cond> else <expr>, 27.25% for `<cond> ? <expr> : <expr>`, and 34.25% for `if <cond> { <expr> } else { <expr }` we did also look at a few other styles like: `if <cond>: <expr> else: <expr>`, but we decided that the python style is the best for a simple reason, its easier for new dev's who want to come into systems programing or writing low level code. If a lot of people do really hate it, we can swap it out before an alpha it would be a really simple AST node parser change...
I don't know if your language is expression based like Rust, but in Rust the nice thing is that it's just the regular if syntax being used as an expression and not some additional special case syntax, which is elegant.

I actually like the Haskell syntax more because it's looks nicer when formatted in multiple lines:

  -- Haskell:
  -- single line
  if ... then ... else ...

  -- multiline
  if ...
      then ...
      else ...

Where in Rust with normal formatting it because 5 whole lines:

  // Rust:
  let result = if ... {
      ...
  } else {
      ...
  };

  // or, 4 lines but less common:
  let result = match ... {
      true => ...,
      false => ...,
  };
I think the strongest argument for condition-first that's unrelated to "taste" is the fact it is consistent with the order of execution:

  # Python:
  # you have to read "from the middle out" to read with the order of execution
  x = a() if b() else c()

I also strongly suggest you look up why Python ended up with the syntax it did. The reasons were not really good and not really applicable to a new language. Maybe my memory is bad because I can't find the source, but I remember one of the justifications was simply that it turned out faster in cpython (the python interpreter) in the test implementation they did.

I might also consider asking Chris Lattner (Mojo language). They did use that syntax in Mojo, because it is made to be a superset of Python. But I remember hearing him say he really disliked it.

I also found the conditional syntax (with the condition in the middle) a bit awkward compared to Rust's or even C's. But there is certainly a common operation. And this is the syntax chosen by the Python language designers.

I'm curious. Why do you think it is bad? What are the possible issues that this style can cause?

I think I only started to really hate it when I had to deal with some Python code where people decided to nest them and made a really hard to read mess where they should have used just a normal if-else chain. A Rust style if-expression would be better IMO, but it was Python code.

On a more basic level, I just don't see any good reason to put the condition in the middle.

Lisp: `(if <cond> <then> <else>)` Haskell: `if <cond> then <then> else <else>` Rust: `if <cond> { <then> } else { <else> }`

All the above read fine and normal. In the python version the order of execution is different then the left-to-right reading order.

It's not like it's a fatal flaw for a language to do it like Python. But it is just silly IMO. My POV is that I actually really like if-expressions, but hate the condition-in-the-middle syntax, so I tend to avoid it unless it's really short. When code changes slightly and becomes longer I'd rather not have to always double take if this now should change to an if-statment or to an expression etc.

> I think I only started to really hate it when I had to deal with some Python code where people decided to nest them and made a really hard to read mess where they should have used just a normal if-else chain.

Oof! I can see how that can get really messy really fast. Though I wonder how much of it is the language's fault as it is the programmer's. I used to tell my students to avoid trying to be too clever with the source code. Source is meant for humans to read. Leave the cleverness to the interpreter.

> On a more basic level, I just don't see any good reason to put the condition in the middle.

Thinking about it again, I think I understand why the language designers chose that order. They were trying to make it make it more readable by following the structure of the English language. "<Buy a steak> if <the butchers are open>. Otherwise <get some salmon>".

The problem here is that programmers are more accustomed to the natural and somewhat obvious order that every other programming language follows. They also wouldn't have expected some people to abuse it by nesting it.