|
|
|
|
|
by jriddycuz
6067 days ago
|
|
Yes, but these warnings can generate false positives if you actually want to do assignment in a test condition. And before everyone tells me that's bad coding style, it is at least a common idiom in certain kinds situations like this very common example from PHP (whose compiler does not warn for assignment at all): <?php
if (false !== ($pos = strpos($haystack, $needle))) {
doSomethingWith($haystack, $pos);
}
?>
I find the constant first notation to be very friendly, btw. It's not as natural when translated into English normally, but in terms of logic, it makes sense to think of the first part of the test condition as its own little predicate: // Generic language
bool matchesMyConstantValue(val) {
return MY_CONSTANT_VALUE == val;
}
|
|
I find assignment-in-conditional more useful in loops than if's, since you can't just plonk the assignment on the previous line.