Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> What's the difference between ternary operator and inline if/else?

There is no difference. Ternary operator is a syntactic sugar for if/else.

> Is there even any reason for if/else that doesn't return a value (that may be void/unit/similar)?

Main reason why an if statement returns a value is because it is used to test if condition is true or false. If it returns other values, it was going to make it more complicated. Especially when you have an if statement with multiple conditions that must be ORed or ANDed.

What is awful about the variable syntax? This syntax was copied directly from C language.



> Main reason why an if statement returns a value is because it is used to test if condition is true or false.

Uh, no. The condition itself is true or false. The return value is useful for assigning, passing as parameter, etc. Non-returning if requires dealing with temporary variable, which itself is verbose, doesn't automatically get assigned in both branches, doesn't play nice with C++-style move semantics, etc.

> What is awful about the variable syntax? This syntax was copied directly from C language.

C language is awful, but at least they have the excuse that nobody knew better in the 70s and const kind of works even the syntax is a bit complicated.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: