I often argue with the voices about the correctness of having assignations within an if statement (or equivalent), meaning having this:
if($thing = someFunction()){
//do stuff
}
Instead of this:
$thing = someFunction();
if($thing){
//Do stuff
}
A part of me says its a bad practice because:
- If read quickly (by someone who is not that familiar with the practice) it might be hard to notice that its not a
==, and look like a mistake. - It is not possible in all languages (IIRC, Java explodes with that).
But the voices say its good because:
- Less lines, looks cleaner.
- The practice is not that uncommon (C/C++, PHP).
- The variable acquires more semantics (it generally won't be used outside the
ifscope) - It is actually easy to get used to reading it and not miss it the after seeing it a couple of times.
So, could the voices be right?, or, is it an aberrant practice?.