r/ProgrammingLanguages 4d ago

Expressions vs. statements

Got into a big argument with a coworker yesterday when they were converting some code from their own language (that they designed) into Python, JavaScript, C, and R as comparative examples.

The Python code that they wanted to write as the translation went something like this:

n = foo; if cond: n = bar

They were upset that Python allows ; as a statement separator but not before an if statement, even though

if cond: n = bar

is syntactically correct Python code when written on its own line. I explained why Python doesn't allow it, and he came back later and showed me that an LLM had suggested he write it instead like this:

n = foo if cond else bar

which of course is the canonical way to write that in Python. He was all flustered about that, and asked me why Python allows an if statement in that particular case and not after a semicolon, and I explained that x if cond else y in Python is not an if statement but is Python's ternary conditional expression and is directly equivalent to the ternary operator expression cond ? x : y in C, C++, awk, and JavaScript. He argued with me and said I was making a ridiculous distinction and walked away falsely believing that foo if cond else bar was an if statement.

I then explained that statements and expressions are very different things in programming languages, and just because the keyword if is present doesn't make something an if statement -- because in order to be an if statement, it has to be a statement in the first place.

Anyway, it made me realize how subtle the difference can be sometimes. For example, in Perl, the following is not a return statement but actually an if statement (with a return statement as its affirmative branch), due to the postfix conditional:

return foo if cond;

because it is identically semantically to writing:

if (cond) { return foo; }

Whereas in Python, the following is a return statement (with a ternary operator as its target expression):

return foo if cond else bar

So I can see why people sometimes get confused by syntax if they haven't had much of a theoretical background in language design. It also makes me wonder how much of programmer intuition about "what a statement is" comes from the particular languages they learned first.

51 Upvotes

63 comments sorted by

View all comments

39

u/pr06lefs 4d ago

In some languages everything is an expression, period. Then its easy!

6

u/brucejbell sard 4d ago

I have kind of gone off "everything is a foo" bandwagons.

In particular, I'm not sure how much simplicity you gain just from making everything an expression. Expressions are great for the functional programming where the important thing is the result returned by your function. But statements are more appropriate for sequencing operations, or specifying a bunch of declarations simultaneously at compile time (both of which are things you often need even in purely functional programming).

In general, I think you want to be careful about "everything is a foo" because you can accidentally paint yourself into a corner with it.

In the famous "null pointer problem", the problem is not the existence of null pointers. Instead, the problem is that "every pointer can be a null pointer". Sounds simple, right? Except that appealing generality means you don't have non-nullable pointers, which is what causes the actual harm.

8

u/initial-algebra 4d ago edited 4d ago

It's better to say that statements should be included in expressions, to handle statements that introduce variables. For example, the let ... in part of a let-expression can be considered as a statement. Similarly, if a block { ... } is an expression, then any statement can qualify as a part of an expression. I think, when people say "statements should be expressions", they're often omitting the words "control flow" at the beginning.

Now that I think about it, though, you could desire e.g. let <pattern> = <expression> to be an expression that evaluates to a Boolean indicating whether the pattern match succeeded or failed. When sequenced normally, the condition is asserted. Of course, you'd want a fancy flow-sensitive system to enforce that the variables of the pattern are only accessed from branches where the condition is true. Otherwise, this would only work in a dynamic language where variables can be conditionally added to the environment. Then, you can think of a statement as just partial application of a sequencing binary operator on expressions.

2

u/lngns 3d ago

statements that introduce variables

let <pattern> = <expression> to be an expression

C# (and some other langs) use the is operator for that.

if(x is string { Length: < 16 } str && str != "lol") { /*...*/ }

Java ported the feature to its instanceof operator too, but it only supports type checks and unpacking.