*(c ? &x : &y) = v;
return (char *[]){"No", "Yes"}[!!x];
(Note: for the second one, I use the "selecting from a temporary array" and !! separately; if I only had two options then I'd use x ? "Yes" : "No" of course.) *(c ? &x : &y) = v;
return (char *[]){"No", "Yes"}[!!x];
(Note: for the second one, I use the "selecting from a temporary array" and !! separately; if I only had two options then I'd use x ? "Yes" : "No" of course.)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.
Screen space is cheap. Say what you mean. Clever today is usually a headache tomorrow.
yes
yourCode = bad;
temp = bad;
bad = good;
good = temp;
conclusion = (yourCode == bad)
As we can see from evaluating this code, in this context, it isn’t bad.In general, if something is bad you can change that thing or change the context. Your job, your living arrangements, etc. People often are slow to change the context, even when a small change can make all the difference.
You might want to create an include file if you have a lot of code to check.
mycodeisalwaysgood.h seems self-explanatory.
I switch between many languages often enough so anything even remotely esoteric gets forgotten instantly and causes my brain to pause at suspicious line and making me think too much about the trees instead of the forest.
If however I was only using C and nothing else maybe I'd be catching this bug myself, one never knows ;)
Do you know why need to use pointer magic here?
Tried (c ? x : y) = v; and wondering why that doesn't work.
Is !!x equivalent to x here?
It fits semantically with "I'm using x as a boolean, but it may not be 1".
I use !=0 in only one context, if(strcmp(a,b) != 0) to check if two strings differ. Normally if an if() expression I don't check explicitly for 0 or not 0, except for strcmp class of functions, because of the ordered comparison of it (<0, 0, >0) , its "boolean" value is inverted, so somewhat counter intuitive if used with implicit boolean values.
Cast to bool has me extremely nervous because ... It works in C99's _Bool type, but I have lots of bad memories of security bugs when using this approach with pre-standard custom bool types, which are still in common use including but not limited to Win32 and COM, or Objective-C.
The bug I am thinking of looks like this:
long long ll = 1LL << 32;
bool b = ll;
puts(b ? "yes" : "no");
I just tested to confirm. This prints "yes" when using C99 stdbool, presumably because they standardized it to work. If you change bool to a common choice for pre-standard bool typedefs (int or char, say), it prints "no" because the large nonzero value doesn't fit in that type. "Just use the standard type" you might say. But if we were working somewhere that required legacy nonstandard bools that is not our choice, and further, a later refactor on may start using those other types some day even if we make the correct choice today. So there is an argument to be consistent about avoiding this type of bug.Any nonzero expression is true, and 0 is false. But the result of !, or ==, ||, or any of those other boolean operators is 1 for true.
Recall relatedly that C didn't have a bool type until the 1999 standard, which introduced <stdbool.h>. C++ had it earlier, but for a long time the C way to store a boolean expression into a variable might have been int, or some nonstandard typedef. This is why so many libraries and frameworks in the C world define their own boolean type.