3 ms·
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 addit
by vcarl 12y 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.
- Flimm 12y agoYes, 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.
- ufo 12y agoSure, 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.
- macspoofing 12y ago>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.
- david-given 12y agoAda 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 https://cowlark.com/2014-04-27-ada