Hacker News new | ask | show | jobs
by vcarl 4124 days ago
This is like the

    if (x = true) {}
kind of error. Yeah it sucks, but it's not really the language's fault.

Not to mention that that large a block of addition is bad code smell to me.

2 comments

Yes, it is the language's fault. The language could have been better designed, here are some solutions:

1. Assignments in conditions could be required to be surrounded with an additional pair of parentheses, like this: `if ((x = true)) {}`. GCC with warnings already requires this for C.

2. The assignment operator could be something other than the equal signs, for example, it could be `:=`. Assignment is so different from mathematical equality and beginners to programming trip up on this all the time, it's a shame programming languages copy each other for familiarity and keep this bad design.

3. Assignments in conditions could be banned out right, like Python does.

Sure, but detecting assignments inside conditionals is very easy and every compiler worth its salt gives warnings for them.

IMO, the really annoying language faults are the sort of thing that can't be fixed by a simple linter: object keys being converted to strings, the wonky pseudo-classy prototypal inheritance, dealing with libraries that abuse Function.toString or eval, etc.

>Yeah it sucks, but it's not really the language's fault.

Imagine a language were assignments were only done via := and comparisons with = or ==. A programmer would never encounter this error.

Ada does precisely this. (It also doesn't support assignment inside expressions at all.)

Modern Ada's a thoroughly nice language; I did a writeup: https://cowlark.com/2014-04-27-ada