CodeSOD: Tern Down Service

This post was originally published on this site

The Daily WTF

In C, it’s not uncommon to define a macro like this one:

#define MIN(a,b) (a>b?b:a)

It’s useful to be able to quickly find the smallest of two numbers, and it’s useful to do that with something a bit more readable than a ternary.

Of course, if you need to expand this to larger sets of numbers, it gets tricky. For example, maybe you need to find the smallest of three numbers.

Agripina recently had to track down some strange behaviors in an IoT device, and found this stack of ternaries:

int lowestVal(int a, int b, int c){ return a > b > c ? c : b ? b > c ? c : b : a; }

Three question marks is the mark of a great ternary mangling. The complete lack of any parentheses to group the expression to provide any sense of the actual logical flow is also

To read the full article click on the 'post' link at the top.