Computer Science Canada The ternary operator: redemption |
Author: | wtd [ Sun Dec 25, 2005 9:51 pm ] | ||||||||
Post subject: | The ternary operator: redemption | ||||||||
Is the ternary operator just an ugly piece of legacy syntax, or can it be useful, and even elegant? Imagine something like:
Well, this is pretty easy to convert.
But... what if we have lots of "else if"?
This can become:
So, what are the benefits to this? The benefit is really just one I've talked about a million times before. I only write "foo =" once. This makes it much easier down the read to change the name of the variable, or if you prefer "refactor" the code. |
Author: | md [ Sun Dec 25, 2005 11:28 pm ] |
Post subject: | |
I've never used the ternary operator before... but that last example is way nicer then the equivalent if statement... I didn't ever even think of doing it like that before... nice! |
Author: | wtd [ Mon Dec 26, 2005 2:19 am ] |
Post subject: | |
This is the beauty of expressions, as opposed to statements. |