C# 8.0 came out 6 years ago at this point, and it’s syntax is aping that of Javascript’s ES6 update which is 10 years old, which was in turn aping that of earlier functional languages. There are a lot of engineers who have learned how to code using predominantly modern syntax, so the one on the right is “textbook” to them.
And being textbook isn’t a reason to keep doing something forever. The syntax on the left is overly verbose and leaves more room for unexpected behaviour-changing lines of code. The syntax on the right is concise and scannable in a way that doesn’t require jumping back and forth between lines to follow.
I prefer if-expressions where possible. For example, this is valid Rust:
letx = if is_y {
y
} else {
z
};
(Can also be on a single line.)
This is the same syntax as the normal if-statement, except the compiler forces you to add an else-branch, if you want to ‘return’ a value from it.
Don’t tell anyone, but the ternary operator is when the C designers realized that being purely procedural is cumbersome AF. 🙃
Unfortunately, they decided that expressions need to look like math, so now JS devs get to write random question marks and colons across many, deeply nested lines of code.
deleted by creator
C# 8.0 came out 6 years ago at this point, and it’s syntax is aping that of Javascript’s ES6 update which is 10 years old, which was in turn aping that of earlier functional languages. There are a lot of engineers who have learned how to code using predominantly modern syntax, so the one on the right is “textbook” to them.
And being textbook isn’t a reason to keep doing something forever. The syntax on the left is overly verbose and leaves more room for unexpected behaviour-changing lines of code. The syntax on the right is concise and scannable in a way that doesn’t require jumping back and forth between lines to follow.
Boiling down multi-line expressions into single line statements has been a trend in Comp-Sci for a while.
That
format has been around for decades.
I generally prefer it to clunky if-statements
I prefer if-expressions where possible. For example, this is valid Rust:
let x = if is_y { y } else { z };
(Can also be on a single line.)
This is the same syntax as the normal if-statement, except the compiler forces you to add an else-branch, if you want to ‘return’ a value from it.
Don’t tell anyone, but the ternary operator is when the C designers realized that being purely procedural is cumbersome AF. 🙃
Unfortunately, they decided that expressions need to look like math, so now JS devs get to write random question marks and colons across many, deeply nested lines of code.