From the "Huh?" files...
-
I don't think this really counts as a full-on WTF, but when I ran into it it still took me several seconds to parse what I was looking at (all values are doubles, and there's no operator overloading or anything crazy like that going on, this is just bog standard silliness):
someValue += -1 * someOtherValue;
Just a bit of strangeness I found spelunking in the code base that I thought you (guys | gals | hyperintelligent shades of the color blue) would appreciate.
Filed under: but why do it like that, I guess it works, wat
-
Out of curiosity, what are the normal values that one could expect for
someOtherValue
?
-
Out of curiosity, what are the normal values that one could expect for
someOtherValue
?
Less than one - it's basically a weighted value. I'd have to go looking at the code again to be more specific.
-
Less than one - it's basically a weighted value. I'd have to go looking at the code again to be more specific.
So that's technically a reduction of a reduction, no?
-
Insofar as I remember, you are technically correct (the best kind of correct) but that's not the issue that I have with this snippet.
-
Off the top of my head, I'm not sure what the operator precedence would be there. I'm guessing
someValue += (-1 * someOtherValue);
because
(someValue += -1) * someOtherValue;
would make no sense at all
-
operator precedence
Assignments are always evaluated after logic and arithmetic operators in C-like languages.
-
You're right, but... Pop quiz:
How is
a & b == c
evaluated in C?[spoiler]As you may be able to guess by the fact I'm asking (or you just know it), if you said
(a & b) == c
, you're wrong: it'sa & (b == c)
. Like everything that is terrible about computers, this was the result of a somewhat reasonable short-term decision early on in C's life that turn out to be the wrong one long-term but was kept around in the name of backwards compatibility.[/spoiler](This is not really apropos of nothing; just a fact that I ran into just yesterday -- not due to a bug, thankfully -- that I think I didn't previously know.)
-
someValue += -1 * someOtherValue;
This is not that bad. It would be if the variables were enums. But they're doubles. So aside little bit of redundancy, everything's fine.
-
Is there a reason in this language not to use someValue -= someOtherValue? Or is the language incapable of -=?
-
Is there a reason in this language not to use someValue -= someOtherValue?
No. All languages that have += also have -=.
-
Is there a reason in this language not to use someValue -= someOtherValue? Or is the language incapable of -=?
This is actually what I was getting at. There's nothing wrong with this, it just seemed silly.
-
How is a & b == c evaluated in C?
This sort of thing is why Practical C Programming recommends using parens for everything besides the basic arithmetic operators (
+-*/=
).
-
Probably someone had told the developer about += and not thought to explicitly point out that it works with - as well.
(S)he probably thought they were being terribly clever finding a way to use += to subtract.
-
And forgot that unary
-
exists ...
-
I'm not sure what the operator precedence would be there
Assignment operators are very low precedence in most languages that have them.