Carney accuses Trump of being too 'emotional' after president canceled trade negotiations over Reagan ad
Noel Gallagher's handwritten lyrics for Oasis hit Don't Look Back In Anger are sold for FIVE times their value as bidding war climbs to £20,000
Trump's lavish praise for Japan's first female Prime Minister Sanae Takaichi after he receives grand welcome in Tokyo: 'That's a big deal'
Locals struggling with bad smell after 700k tonnes of toxic waste dumped on Essex farm
Rachel Reeves' £2million mansion tax: How could it work and what would it do to the housing market?
Threat of a mansion tax sparks house market havoc... as Housing Secretary refuses FOUR TIMES to rule out new socialist levy in Budget
Meghan Markle's exact travel bag is available to buy - and it's perfect for your next getaway
One in three of today's workers faces poverty trap in retirement
Essex locals fear they are being 'punished' as 3,000 homes allocated for town
ExxonMobil Accuses California of Violating Its Free Speech
Read more of this story at Slashdot.
Automattic accuses rival WordPress outfit WP Engine of ‘false advertising, and deceptive business practices’
UPDATED The long battle between Automattic and WP Engine has flared again, this time with accusations the latter company issued “false advertising”, and employed “deceptive business practices.”…
IBM Cloud stops signing and seeking new customers for its VMware service
IBM has announced it will stop marketing its VMware on IBM Cloud service to new customers.…
The millionaires at war with Sandbanks dog walkers over their 'private' beach after their fence sparked fury
CodeSOD: A Truly Bad Comparison
For C programmers of a certain age (antique), booleans represent a frustrating challenge. But with the addition of stdbool.h, we exited the world of needing to work hard to interact with boolean values. While some gotchas are still in there, your boolean code has the opportunity to be simple.
Mark's predecessor saw how simple it made things, and decided that wouldn't do. So that person went and wrote their own special way of comparing boolean values. It starts with an enum:
typedef enum exop_t { EXOP_NONE, EXOP_AND, EXOP_OR, EXOP_EQUAL, EXOP_NOTEQUAL, EXOP_LT, EXOP_GT, EXOP_LEQUAL, EXOP_GEQUAL, EXOP_ADD, EXOP_SUBTRACT, EXOP_MULTIPLY, EXOP_DIV, EXOP_MOD, EXOP_NEGATE, EXOP_UNION, EXOP_FILTER1, EXOP_FILTER2 };Yes, they did write an enum to compare booleans. They also wrote not one, but two functions. Let's start with the almost sane one.
static bool compare_booleans (bool bool1, bool bool2, exop_t exop) { int32_t cmpresult; if ((bool1 && bool2) || (!bool1 && !bool2)) { cmpresult = 0; } else if (bool1) { cmpresult = 1; } else { cmpresult = -1; } return convert_compare_result(cmpresult, exop); }This function takes two boolean values, and a comparison we wish to perform. Then, we test if they're equal, though the way we do that is by and-ing them together, then or-ing that with the and of their negations. If they're equal, cmpresult is set to zero. If they're not equal, and the first boolean is true, we set cmpresult to one, and finally to negative one.
Thus, we're just invented strcmp for booleans.
But then we call another function, which is super helpful, because it turns that integer into a more normal boolean value.
static boolean convert_compare_result (int32_t cmpresult, exop_t exop) { switch (exop) { case EXOP_EQUAL: return (cmpresult) ? FALSE : TRUE; case EXOP_NOTEQUAL: return (cmpresult) ? TRUE : FALSE; case EXOP_LT: return (cmpresult < 0) ? TRUE : FALSE; case EXOP_GT: return (cmpresult > 0) ? TRUE : FALSE; case EXOP_LEQUAL: return (cmpresult <= 0) ? TRUE : FALSE; case EXOP_GEQUAL: return (cmpresult >= 0) ? TRUE : FALSE; default: printf( "ERR_INTERNAL_VAL\n" ); return TRUE; } }We switch based on the requested operation, and each case is its own little ternary. For equality comparisons, it requires a little bit of backwards logic- if cmpresult is non-zero (thus true), we need to return FALSE. Also note how our expression enum has many more options than convert_compare_result supports, making it very easy to call it wrong- and worse, it returns TRUE if you call it wrong.
At least they made booleans hard again. Who doesn't want to be confused about how to correctly check if two boolean values are the same?
It's worth noting that, for all this code, the rest of the code base never used anything but EXOP_EQUAL and EXOP_NOTEQUAL, because why would you do anything else on booleans? Every instance of compare_booleans could have been replaced with a much clearer == or != operator. Though what should really have been replaced was whoever wrote this code, preferably before they wrote it.
Justin Bieber reveals what he classifies as cheating during most candid chat ever about marriage to Hailey
Struggling Essex hospital service to be moved to local shopping centre
David Foster, 75, looks exhausted as he takes his much-younger wife Katharine McPhee, 41, to dinner
Subtle digs Katy Perry and Justin Trudeau sent to their exes during first public date, according to a body language expert
Charlie Sheen says Denise Richards still loves him nearly 20 years after explosive divorce
Twist in Tesco vs. VMware case as Computacenter files claim against Broadcom, Dell
Tesco’s lawsuit against VMware has taken a twist, with Computacenter filing a claim against Broadcom and Dell.…