NiteCyper
NiteCyper

Reputation: 111

Is there a way to replace the contents of two variables with each other('s)?

Without using a third variable for temporary storage and variables contents rotation? Such as a special equal(-sign) operator?

Efficient code is beautiful code.

Upvotes: 0

Views: 106

Answers (1)

Astrogat
Astrogat

Reputation: 1625

Readable code is beautiful code, so there really isn't a better way than using a temporary variable.

If you really wanted to do it you could use a few xors.

Since

X := X XOR Y
Y := X XOR Y
X := X XOR Y

switches the two variables. You can do xor with ^ in autohotkey.

However, do compare it to:

c = a
a = b
b = c

It's just as long, but the using a temporary variable is much more readable. And the cost of creating a variable isn't going to matter, no matter the system you are making (especially if you are making it in AutoHotKey).

Upvotes: 2

Related Questions