3 ms·
Assignments being expressions is frequently very useful for avoiding to write the same expressions twice, which could also be a source of errors. You are right
by adrian_b 5y ago
Assignments being expressions is frequently very useful for avoiding to write the same expressions twice, which could also be a source of errors.
You are right about the errors caused by unintended assignments, but the root cause for these errors is not the fact that assignments are expressions, but the fact that in B & C they have replaced the operators inherited by BCPL from ALGOL, ":=" for assignment and "=" for equality test with "=" and "==". With the original operators, typos would have been very unlikely.
This misguided substitution was justified by less typing, but a much better solution would have been to not change the syntax of the language but to just modify their text editor to insert ":=" when pressing ":", because deleting the "=" when not needed would have been required very seldom, as ":" in labels or "?:" is encountered much less frequently than both assignments and equality tests.
- agent327 5y agoIf you have the same expression twice as part of a compound assignment, there's nothing stopping you from assigning the variable instead of repeating the expression: a = b = f(); vs. a = f(); b = a; And tangential, but editors really shouldn't be mangling text as you type. Whenever I install Visual Studio anywhere, it's the first thing I turn off...