4 ms·
I don't find those obfuscatory at all either, and instantly understood them upon first reading. I think they are a form of "data-oriented" style of programming,
by userbinator 7y ago
I don't find those obfuscatory at all either, and instantly understood them upon first reading. I think they are a form of "data-oriented" style of programming, where array indexing and selection via ?: are preferred over "code-oriented" control flow statements. In my experience, the former actually tends to create more concise and maintainable code, since it often means a lot of the logic becomes table-driven and easily modified. The latter tends to result in very long and "branchy" code with lots of if/else statements that ultimately contain tiny bodies that don't do much.
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."
- jackewiehose 7y ago> I don't find those obfuscatory at all either, and instantly understood them upon first reading 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.
- gpderetta 7y agoFWIW, !! is a fairly idiomatic way to cast a value to bool.
- jackewiehose 7y agoInts are automatically converted to bool, there is no need to cast. What this does is it restricts the int to 1 or 0. The clean way to write this is x ? 1 : 0. This is understandable even if you don't know the Boolean inversion of 0 is 1 (Which isn't self-evident. For example in Basic its -1).
- gpderetta 7y agoThey are converted to bool in bool context. Outside of that you have to force it. 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.
- jackewiehose 7y agoYou don't have to enforce a conversion from int to bool if you just want a bool. In this example you don't want to increment by 'bool', you want to increment by 1 or 0 (which are ints). So you use !!x to convert to bool and back to int. 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.
- gallier2 7y agoYou can also typecast to bool since C99. (bool)x if you include <stdbool.h> or (_Bool) if not.
- userbinator 7y agoedit: not a great example as values[1] >0 would be better. Only if that's an array of unsigned ints, as otherwise the code would count only positive nonzeros.
- entelechy0 7y agoAlmost universally, every employer I've ever worked for, and consequently, every student I've ever taught, understands if-then-else statements more easily than ternary operators.