- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
TRWTF is not writing a macro to rewrite
a > b > c
intoa > b && b > c
. After all, some people have managed to implement sorting using nothing but macros.Admin
I think the macro should be more like:
#define MIN(a,b) ((a)>(b)?(b):(a))
in case a or b have an operator lower-precedence than ">" in them (though that's a much lower risk than the analogous one for a "square" macro). It still doesn't work right with expressions that have side-effects.
The WTF code also uses "b" as a condition later on in the expression, which is surely wrong too.
Admin
int lowestVal(int a, int b, int c){ return (a<b) > (a<c) ? c : (a<c) > (a<b) ? b : a; }
Fixed.
Admin
I'm thinking that the person who wrote "a > b > c" is more likely to be a Python programmer than a mathematician. This dubious feature of Python confuses students all the time. It also leads to some weird, inadvertent cases even in Python:
See https://github.com/cosmologicon/pywat
Admin
MIN(MIN(a, b), c)
...I heard you like macros, so I put macros in your macro
Admin
And we can't forget about the "fun" those macros can cause when you're comparing, say... MIN( 1, someFunctionWithLotsOfSideEffects() );
"Why are we sometimes getting duplicate entries in our database?" "Who cares, ship it!"
Admin
You can also use the standard library:
std::min({a, b, c})
Admin
std::min is C++, not C.
Admin
I will not. I avoid situations in which I need to roll a saving throw versus my sanity.
Admin
Not fixed. lowestVal(2, 2, 1) returns 2.
Admin
Well.... that code is neither an Elegant tern nor a Common tern
Admin
I'd also make it inline. With a decent compiler that should be equivalent to a #define without the problems with doubly evaluated parameters.
inline int lowestVal(int a, int b, int c){ return a > b ? b > c ? c : b : a > c ? c : a; }
Admin
/* only works for strictly orderable integers */
Fixed.
Admin
Clearly one tern deserves another.
Admin
The implementation of MIN in the standard library I use stores the values into two local variables. As a result, you can calculate MIN (x++, f (y++)) and it will evaluate both expressions once and return the correct value.
Admin
Am I missing a joke here? I don't read "a > b > c" as one term in this line. It is probably meant to say a > (b > c ? c : b) ? (b > c ? c : b) : a
Admin
Came here to say that - that's the biggest WTF with macros, usually. You MUST paren every parameter everywhere it appears, because inevitably someone will invoke it with a complicated expression and precedence will make it fail miserably.
Wrong: #define FOO(x,y) x + y Right: #define FOO(x,y) ((x) + (y))
But by convention you're on your own for side effects, so don't do it.
Admin
The biggest WTF with macros is using the stupid things in the first place. They are the most braindead, retarded, spastic, crippo, thaliddy idea ever made.
Admin
Hard to read macros or a really simple function:
int lowestVal(int a, int b, int c){ min = a; if(b<min) min = b; if(c<min) min = c; return min; }
Admin
If you think that's bad: In the build process of the computer game Nethack, one of the source files (the monster definitions) is compiled twice with different sets of macros enabled to produce two of the the main data tables.
Admin
We got caught out a few times here. We had a short-cut for a commonly used method as #DEFINE DoStuffA if (someCondition) DoStuff.
Works well, unless you put that in the middle of an if-else statement. As in, if (something) DoStuffA() else DoSomethingElse(). The "else" isn't being applied to the condition you thought it was. The fix was to throw curly brackets around the define.
Admin
So, this is the kind of programmers who code Internet-of-Things software? but more at large, the kind of companies too (no code reviews, no tests)? No wonder there is a dedicated site to IoT called Internet-of-S**t
Admin
Macros are poor man's inline functions. There is no reason to use them for actions such as described in this WTF.
Admin
That very same expression popped into my head
Admin
Only one bracket missing...who can remember all operator precedences ?
a > ( b > c ? c : b ) ? b > c ? c : b : a
Admin
Tern down for what?
Admin
“Multiple exclamation marks,' he went on, shaking his head, 'are a sure sign of a diseased mind.”
― Terry Pratchett, Eric
Admin
Damn, you're right; how did we all not spot that.
Admin
Thank you! Now that darn thing makes sense.
Admin
(And to Little Bobby Tables ):
Nope. Macros are a great abstraction tool which made programming in Common Lisp extremely more fun.
Admin
Agreed, but I don't find it that weird. Operator precedence is a standard thing (https://docs.python.org/3/reference/expressions.html#operator-precedence), and left-to-right evaluation of operators with the same precedence is also fairly standard; it's just that comparison operators are chained rather than evaluated (https://docs.python.org/3/reference/expressions.html#comparisons). I've never worked with another person who can do Python so I may have ten years of bad self-taught info, but I'd generally expect anybody using arbitrary chaining in committed code to ensure that everybody else working on the project was comfortable enough with it to be able to read and understand it, otherwise they should get their brackets/parentheses out (at the risk of evaluating arguments multiple times).
Admin
Solution: do { if (condition) statement; } while (0)
Can be used anywhere.
Admin
What you're missing is that this site is mostly trolls.
Yeah, that's what was meant, but not what this does. Everyone here but you is laughing at what it does versus what was intended.
I think this is one of the cases where it's better to just do MIN(a, MIN(b,c)), and if that has unwanted effects due to side effects of a, b, and c, either use a real function or use a real function to wrap MIN(a, MIN(b, c)). That said, I expect even that was a problem, so they exploded out what the macros were doing by hand, not realizing that the problem was the macro lacked any parentheses, so would be wrong in all but the most trivial cases.
Admin
this is what you get with weak typing
Admin
10 Things That Your Family Taught You About Only Fans Pornstars Kayleigh Wanless only fans pornstars Kayleigh Wanless