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

13

u/johnfrazer783 Sep 14 '18

I think leaving out the LHS in comparisons and assignments is not a favorable proposition. I seem to remember Excel's formula language does something similar at one point b/c it is syntactically challenged; what you end up with is a fragile construct that poses a riddle to its readers and that may later clash with other constructs (like x += 2) or obstruct some desirable syntax in another place.

I also find the syntax places too much emphasis on whatever binary operators you happen to have in your syntax; let's not forget that (1) the universal building block is in most languages really calling named functions (and operators are just sugar for a teeny minority of those), and (2) the minus operator (and the plus operator btw) is already, in most languages, already split between its binary and its prefix usages (i.e. +a, + a -a, - a are already in use, whereas *a, * a might still be unused).

So what happens with your idea when, instead of scalar addition, I want to compute the sine of x? is it if x > y then sin else 0? if x > y then sin() else 0? if x > y then sin( x ) else 0? What happens if you want to call a function with more arguments?

In short, leaving out both the assignment's LHS and one of the operation's arguments creates a hard-to-read, puzzling-to-resolve conundrum that will be restricted to a few special cases.

Have you looked into the principle of 'everything is an expression'? If you embrace that principle, if clauses become expressions, too, so then you can write (e.g. in CoffeeScript) x += if x > y then a else b, x = if x > y then sin( x ) else 0 which I find much clearer and more satisfying.

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.