News for hackers. Stuff that matters.
Re:Something simple I wish I learned 17 years ago by srn on Monday November 02, 2009 @11:30AM (#917087)
Good compilers will warn you.

Extend this to functions as well.

I had a bug like

if (foo(b == A))

instead of

if(foo(b) == A)

Re:Something simple I wish I learned 17 years ago by cperciva on Monday November 02, 2009 @11:35AM (#917108)
Good compilers will warn you.

Even most bad compilers warn about this now.

Re:Something simple I wish I learned 17 years ago by oscardelben on Monday November 02, 2009 @11:48AM (#917153)
Even ruby warns you

puts 'argh' if a = 'b' warning: found = in conditional, should be ==

Re:Something simple I wish I learned 17 years ago by jriddycuz on Monday November 02, 2009 @12:21PM (#917266)
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;
    }
Re:Something simple I wish I learned 17 years ago by Freaky on Monday November 02, 2009 @01:23PM (#917446)
When gcc warns you, it tells you to wrap it in parens if you really mean it: "warning: suggest parentheses around assignment used as truth value". Your PHP example wouldn't raise a warning from gcc, because it already is.

I find assignment-in-conditional more useful in loops than if's, since you can't just plonk the assignment on the previous line.

Re:Something simple I wish I learned 17 years ago by encoderer on Monday November 02, 2009 @12:57PM (#917361)
Agreed and a more general case of that is something like:

  if ($result = $o->someMethod()) {
	// Do Something with $result, the method returned a value that cast to true
}
Re:Something simple I wish I learned 17 years ago by mattiss on Monday November 02, 2009 @01:14PM (#917419)
I would argue that this is pretty unreadable. Why do obfuscate your code to save one LOC?
Re:Something simple I wish I learned 17 years ago by etal on Monday November 02, 2009 @02:25PM (#917641)
It's common in C where functions return an error code instead of a value that you would use again. Then the conditional just checks that you're OK to go ahead, and you don't need to declare another variable to store the error code. (If a language has exceptions, then this isn't as useful.)
Re:Something simple I wish I learned 17 years ago by alttab on Monday November 02, 2009 @02:33PM (#917668)
I write C full time for the day job and it normally goes something like this:

if ( !method_returns_rc( args ) ) { //error handle }

This isn't great, as I believe its not good to put methods with side effects in if statements (may be wrong here, or maybe its just me), but generally thats how its done (not to say that there aren't freaking GOTOs in our code... so take that with a grain of salt.)

Re:Something simple I wish I learned 17 years ago by jriddycuz on Tuesday November 03, 2009 @03:49PM (#920257)
One man's obfuscation is another man's expressiveness.