4 ms·
Pretty sure that's been a warning for a long long time in most compilers. They require you to wrap the assignment to suppress it, that is: if ( (foo=bar) )
by goldenkey 13y ago
Pretty sure that's been a warning for a long long time in most compilers. They require you to wrap the assignment to suppress it, that is:
if ( (foo=bar) )
Anyho, it really depends on what your -W level is set to. It's good practice to use "-Wall"
- tubs 13y ago-Wall doesn't give all errors though, you probably want -Wall -Wextra.
- chris_wot 13y agoThat's still stupid. Put the assignment on its own line.
- goldenkey 13y agoWow, what an insightful comment.
- chris_wot 13y agoLikewise.
- goldenkey 13y agoI hope you're being sarcastic!
- chris_wot 13y agoUnder what circumstances would you place an assignment into an if statement condition?
- goldenkey 13y agoI like doing it in certain cases. It definitely helps in a while loop when you are working with a stream, iterator, or stack and pulling items off while it isn't empty. Not really sure about the if, it's just kind of convenient sometimes. Guess it all comes down to preference. Too much use and it becomes very unreadable.
- chris_wot 13y agoYou just gave me an example of a loop. In what circumstances do you find it convenient, and when do you feel that the advantages of convenience outweigh that of code readability?
- goldenkey 13y agoIf it's a single assignment, then it's merely a preference. I cannot think of a case where it is explicitly better for a single conditional. However, a compound conditional can actually be more clear if the assignment is there in many cases. For example: if ( (elt = db.getNextResult) && elt.shouldProcess){ ... ... } In a compound conditional, the assignment is forced to be wrapped in parenthesis, so the ambiguity doesn't exist. Anyho, that's that.
- chris_wot 13y agoWith that if statement, does it first assign the value of db.getNextResult() and then check the elt.shouldProcess variable to see if it is true or false, or does it firstly check to see if elt.shouldProcess is true or false and then load the next result from the database?