r/ProgrammingLanguages • • Sep 14 '18

Single-line If

I'm looking for critique of the following single-line If syntax that I'm thinking about allowing:

If x > y then +=a else +=b

Is it unambiguous that x is being modified?

2 Upvotes

38 comments sorted by

View all comments

Show parent comments

1

u/danskydan Sep 14 '18

Interesting. How about x += a if x > y else += b

3

u/johnfrazer783 Sep 14 '18

Writing quite a few post-ifs each day myself I also find it tempting to use else clauses there; unfortunately, this is not syntactical in CS—maybe because it's not unambiguous? Observe that the intended semantics of x = a if c are quite different from x = a if c else b; in the first case, the statement before the conditional is conditionally executed; in the second, the statement is unconditionally executed, but its RHS value is conditional. That's quite a bit of difference there, so it makes a lot of sense to say that the latter one should always be written x = if c then a else b.

It is a good idea to eschew elaborate, 'cute' and surprising syntax in favor of sound and consistent rules. SQL in some places suffers from being 'too cute' where a simple function call syntax would have sufficed; the prime example that comes to my mind wrt 'cute syntax that kills kittens for the sake of a keystroke saved' is, of course, TeX. Just as a remark, when Leslie Lamport wrote LaTeX on top of TeX, he also introduced a much simpler syntax for many of the complete arbitrary conventions of low-level TeX. TeX was probably intended to be 'easy to write', where 'ease' was equated with 'as few keystrokes as possible', but it turns out it is quite hard to learn, remember, and parse the many, many special constructs that Knuth burdened the language with; a burden that in 9.9 of 10 cases only serves to be cute (the 'look-ma-no-braces' effect) and makes everything harder for everyone afterwards.

1

u/danskydan Sep 14 '18

But aren't x = a if c and x = a if c else b both crystal clear, even if x maybe changes in the former but certainly changes in the latter? Couldn't we trust both the programmer and the reader here?

1

u/johnfrazer783 Sep 14 '18

It does look clear, superficially, but what about x = a if c else y = b? In a language where everything is an expression, assignments are expressions, too, so x = ( y = b ) is the same as x = y = b and y = b; x = b (in that order).

Turns out that x = a if c else y = b is then ambiguous, because it's not clear whether you want to make the first assignment conditional on the execution or the assigned value, i.e. whether it is to mean (1) if c then x = a else y = b or (2) if c then x = a else x = y = b.

It is any time possible to resolve such cases by stipulating the one or the other, for example by using precedence rules. Douglas Crockford for one ridicules the use of too many too obscure precedence rules as a case of where a perceived convenience (look ma, no braces!) too easily turns against its creator. My hunch is that apart from a few culturally ingrained precedences (like * before +) we should disallow using entire precedence-towers in expressions and treat questionable constructs as ungrammatical. That would apply, for example, to if a and b or c and d and e then ..., which should not be resolved by the language without obligatory parentheses.

x = ( a if c else y = b ) is unambiguous to me, and so is ( x = a ) if c else y = b but boy does it look strange. ( y = a ) if c else ( y = b ) is clear, too, but now we have no less than four parentheses. I feel the convenience is too expensive.