2 ms·
Wow, what an insightful comment.
by goldenkey 13y ago
Wow, 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?
- goldenkey 13y agoConditionals are evaluated left to right chris. It's no different than any other conditional. An assignment simply evaluates to the lvalue.
- chris_wot 13y agoAccording to C99, 6.5.16 clause 4, the order of evaluations on operands is undefined and if an attempt is made to modify or access the operand is after the next sequence point then the behavior is undefined.
- goldenkey 13y agoChris, && and || are short-circuited operators that create sequence points. They are exceptions to that rule. except where noted [e.g. special rules for && and ||], the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is Unspecified. This is basic stuff Chris, let's not debate the obvious, this kind of code is all over the place in the Linux kernel.
- chris_wot 13y agoI appreciate your candour. Can you point me to where this sort of thing is used in the Linux kernel? It looks like it is frowned on to me: * http://linux-kernel.2935.n7.nabble.com/PATCH-drivers-net-ifb-Fixed-coding-style-error-assignment-in-if-statement-td705508.html http://linux-kernel.2935.n7.nabble.com/PATCH-drivers-net-ifb... * http://linux-kernel.2935.n7.nabble.com/PATCH-1-4-silicom-checkpatch-assignments-in-if-conditions-td668659.html#a668744 http://linux-kernel.2935.n7.nabble.com/PATCH-1-4-silicom-che... * http://linux-kernel.2935.n7.nabble.com/PATCH-0-4-Staging-silicom-coding-style-cleanup-td669107.html#a669110 http://linux-kernel.2935.n7.nabble.com/PATCH-0-4-Staging-sil... Not to mention, checkpatch.pl checks for assignments in if statements: http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/scripts/checkpatch.pl#n3268 http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.g... I should point out that someone tried to add a backdoor into the Linux kernel source through the use of an assignment in an if statement in 2004. What are your thoughts on this? https://freedom-to-tinker.com/blog/felten/the-linux-backdoor-attempt-of-2003/ https://freedom-to-tinker.com/blog/felten/the-linux-backdoor... Also, where is the place in the C99 standard that details the exception? Genuinely curious. When I reviewed the standard, all that is said about the if statement is in section 6.8.4.1, and that says nothing about short circuited logic, certainly nothing about "exceptions to the rule". This seems compiler specific, but I'm happy to be shown to be wrong - if you can point me to the part in the standard.