The correct way to replace two variables:
decimal tempDecimal = startAngle; startAngle = stopAngle; stopAngle = tempDecimal;
In other words, use a temporary variable.
There you have it. No ingenious tricks, no companions of your code, cursing you for decades, no entries in The Daily WTF , and donβt spend too much time trying to figure out why you needed it in one operation, since at the lowest level even the most difficult a language function is a series of simple operations.
A simple, understandable, easily understandable solution t = a; a = b; b = t; t = a; a = b; b = t; .
In my opinion, developers who try to use tricks, for example, "change variables without using a temporary" or "Duff device", just try to show how smart they are (and fail).
I liken them to those who read books with large books solely in order to seem more interesting at parties (as opposed to expanding your horizons).
The solutions you add and subtract to, or based on XOR, are less readable and most likely slower than the simple "temp variable" solution (arithmetic / boolean-ops instead of simple assembly-level moves).
Do it yourself and other services by writing good, readable code.
This is my rant. Thank you for listening: -)
Aside, I fully understand that this does not answer your specific question (and I will apologize for it), but there are many precedents on SO where people asked how to do something and the correct answer is βDo not do this.β
paxdiablo Apr 30 '09 at 2:05 2009-04-30 02:05
source share