Yeah, this is a good toggle -- although it really depends on what you are doing in your app whether you want to use a given syntax for something like this. You know -- how conceptually close it is to what you're actually doing.
–
Joshua EvensenSep 10 '10 at 18:52

3

+0: Because this answer only offers an alternative, it doesn't answer the question at all.
–
GuffaSep 10 '10 at 19:34

(As you see, a lot of people call this "the ternary operator", but that only means that it is an operator with three operands. As it happens, there is only one operator with three operands in this language, but it still says nothing about what the operator does.)

+1 for being a good sport and letting me edit. Sorry :-)
–
Platinum AzureSep 10 '10 at 19:07

1

I won't downvote, but the idea of having a helper function for this makes me die a little inside. It would be like having a function public int plus(int a, int b) { return a + b };. Just adding redundancy. It might have merit if Java had pass-by-ref.
–
Mark PetersSep 11 '10 at 5:11

I recently (on my own) found a similar answer to one already stated here. However, the simplest and shortest (non-repeating variable name with least code) answer is:

formControl.disabled ^= 1;

This works best in JavaScript when wanting to toggle boolean, DOM-based attributes (for example, a form control/input's disabled property -- going from a non-editable to edit state). After much searching (with no result that I liked) and some trial and error, I found my solution to be the simplest (however, true instead of a 1 would be clearer -- as was previously posted).

Since this syntax isn't very clear, immediately, I would not advise using it very often (I believe it is appropriate when the variable or property makes the context obvious). I have posted this response (instead of making it a comment) because the context in which the XOR bitwise self-assignment should be used is very important. This "trick" should mostly be avoided when considering best practices.

As others have noted, there are two ways to negate something: "lvalue = !lvalue;" and "lvalue ^= 1;". It's important to recognize the differences.

Saying "lvalue = !lvalue" will cause lvalue to be set to 1 if it was zero, and 0 if it was set to anything else. The lvalue will be evaluated twice; this is not a factor for simple variables, but saying "someArray[index1][index2][index3][index4] = !someArray[index1][index2][index3][index4]" could slow things down.

Saying "lvalue ^= 1;" will cause lvalue to be set to 1 if it was 0, 0 if it was 1, and something else if it was neither zero nor 1. The lvalue need only be specified or evaluated once, and if the value is known to be either zero or 1, this form is likely to be faster.

Too bad there's no auto-negate operator; there are times such a thing would be handy.

I wish I could downvote this multiple times. I can only think this wasn't posted in good faith, it's so wrong. I guess for starters I could say != is not an assignment operator, so v != true; won't change v.
–
Mark PetersSep 11 '10 at 5:14