Combining multiple booleans into an integer and using that as a switch or index is another related technique.
To modify an old saying slightly, perhaps "one person's obfuscation is another person's simplicity and elegance."
Combining multiple booleans into an integer and using that as a switch or index is another related technique.
To modify an old saying slightly, perhaps "one person's obfuscation is another person's simplicity and elegance."
I understood the whole list of these obfuscation tricks upon first reading but its still obfuscation.
Creating an array instead of an if-else-switch like the second line is fine but for just two elements and in combination with !!x (and !!x in general) its just nonsense and doesn't help at all.
I'm all for code-density and avoiding repetition so I too use ?: whenever I can but not on the left side of an assignment. That's just an obfuscated if-else-statement. If normal code results in very long and "branchy" code you can rearrange your code in a better way.
int countNonZero = 0;
for (int i = 0; i < values.size(); ++i) {
countNonZero += !!values[i];
}
Also c++ has explicit operator bool();edit: not a great example as values[1] >0 would be better.
The reason I'm pointing this out is that often enough I had to work with code where this distinction was unknown to the author and you come across code like if (!!x) which is, again, just nonsense.
Only if that's an array of unsigned ints, as otherwise the code would count only positive nonzeros.