> it allows you to confuse assignments with comparisons
I don't see a case where such "permission" could lead to any desirable outcome whatsoever, it's lame
Yeah, it's considered bad form to use assignment operators in conditional statements. Even if you know what you're doing when you write the code it could cause confusion for the team that has to deal with it (or even the programmer who wrote once they've forgotten what they meant to do).
As far as I know they didn't design C with the intent of allowing programmers to write convenient but dangerous code, but it just happened to work out that way with the basic syntax they came up with. Later languages like Python reworked the rules so programmers would no longer be able to make that common mistake (or at least make it less likely to happen).
>it's considered bad form to use assignment operators in conditional statements. Even if you know what you're doing when you write the code it could cause confusion for the team that has to deal with it
It also doesn't make much sense, logically speaking
If( myVar is equal to true ) <- self explaining
If( set myVar to true ) <- if what then? If the affectation is a success? What are we checking here?
It basically boils down to if(expression != 0) do_this();
It's up to the programmer to come up with a sensible expression. It's common to use
'while(true) { do_stuff(); if(condition) break; }`
The expressions will be zero (false) or non-zero (true). Usually you wouldn't want a constant expression in a conditional statement, but there are exceptions (to force code to run for debugging purposes for instance).
They do keep working on ways to keep programmers from doing stupid things; but with C it's kind of like you're following policies that were set before OSHA came into being. The scaffolding lets you get where you need to go, but you need to be careful not to fall off while you're working.
(post is archived)