It was thus said that the Great Charlie Carothers once stated:
Perhaps
a bit extreme, but I don't even use the ternary operator in C preferring
the simpler and IMNSHO much clearer if/else construct. I've probably
even replaced the ternary critter with an if/else in code someone else
wrote that I was reworking for some reason. If it takes a microsecond
longer to execute the compiled code, who really cares the vast majority
of the time. OK, if I knew for sure the ternary was faster, I might
grudgingly use it in an interrupt routine but I would not be really
happy about it!
What about the following (real code I've written):
/* example 1 */
if (flags[0] == 'r')
oflags = (flags[1] == '+')
? O_RDWR
: O_RDONLY;
else if (flags[0] == 'w')
oflags = (flags[1] == '+')
? O_CREAT | O_RDWR
: O_CREAT | O_WRONLY | O_TRUNC;
else if (flags[0] == 'a')
oflags = (flags[1] == '+')
? O_RDWR | O_APPEND
: O_WRONLY | O_APPEND;
else
luaL_error(L,"illegal flag: %s",flags);
/* example 2 */
printf(
"; Questions = %lu\n"
"; Answers = %lu\n"
"; Name Servers = %lu\n"
"; Additional Records = %lu\n"
"; Authoritative Result = %s\n"
"; Truncated Result = %s\n"
"; Recursion Desired = %s\n"
"; Recursion Available = %s\n"
"; Result = %s\n",
(unsigned long)result->qdcount,
(unsigned long)result->ancount,
(unsigned long)result->nscount,
(unsigned long)result->arcount,
result->aa ? "true" : "false",
result->tc ? "true" : "false",
result->rd ? "true" : "false",
result->ra ? "true" : "false",
dns_rcode_text(result->rcode)
);
The first example is about the craziest I ever got with the ternary
operator.
-spc (But I still find its intent clear ... )