As for your original question: style #2 is only used by newbies who are
trying to proof that they are able to understand complex syntax rules but
no professional programmer would write code like that because such code is
unmaintainable.
I'd have to respectfully disagree with your characterization of the
conditional operator in C# as both complex and unmaintainable. There's
nothing particularly complex about it. It merely returns one of two values
depending on the evaluation of a Boolean expression. It has been in the C
language family for at least twenty-five years - first in C, then in C++,
and now in C#. Professional programmers have been using it that long, and I
think that few if any professional programmers in this language family would
shy away from it because of any notion that it's unmaintainable. Unless
taken to extremes with large and complex Boolean expressions, it's an
elegantly simple way to evaluate a Boolean test and perform the appropriate
one of two expressions.
Here's a simple example of where it might save you several lines of code,
without loss of transparency of programmer intent: suppose you're preparing
some text for a status bar to report how many email messages your
application has processed, and you want to be grammatical and distinguish
between one message and multiple messages. This simple statement returns
either "1 message" or "N messages", depending on the value of a variable
named count:
String howMany = new String(count.ToString() + " message" + ( (count > 1) ?
"s" : "' );
Elegant, no? As opposed to, for example:
String howMany = count.ToString();
if (count > 1)
howMany += " messages";
else
howMany += " message";
Just consider it a regular part of your syntactic toolkit as a C#
programmer. Use it when it helps you do your job, and always strive for
readable and maintainable code.
Some programmers seem to delight in constructing the most complex and
difficult-to-understand code that they can manage, just to show off. Someone
like that could easily abuse the conditional operator, but they're hardly
behaving professionally when they do.
Tom Dacon
Dacon Software Consulting
a C/C++/C# programmer for twenty-three years, last time I checked