? clear and concise code -> ternary operator

Posted by Sajith M on Jul 21st, 2006
2006
Jul 21

Was having a little discussion when someone said that the ternary operator should not be used because it is difficult to understand and maintain.

condition ? value if true : value if false
This is the syntax of a ternary operator. The condition is evaluated and if true the first value is returned, otherwise the second one is returned. What is so unmaintainable about this? What is difficult to understand about this?

Take this code for example. Can someone tell me what is difficult to understand or maintain
public String MyProperty {
get {
return (ViewState["MyProperty"] == null) ? String.Empty : ViewState["MyProperty"].ToString();
}
}

Or for that matter, how is this code better than the previous one
public String MyProperty {
get {
if (ViewState["MyProperty"] == null)
return String.Empty;
else
return ViewState["MyProperty"].ToString();
}
}

And yeah, I think ternary operator is simply beautiful so please don’t tell me not to use it.

Posted under: Technology/Software Development

Leave a Comment




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.