Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Legible or not: C# multiple ternary operators + Throw if unmatched [closed]

Do you find the following C# code legible?

private bool CanExecuteAdd(string parameter) {     return         this.Script == null ? false         : parameter == "Step" ? true         : parameter == "Element" ? this.ElementSelectedInLibrary != null && this.SelectedStep != null         : parameter == "Choice" ? this.SelectedElement != null         : parameter == "Jump" ? this.SelectedStep != null         : parameter == "Conditional jump" ? false         : false.Throw("Unknown Add parameter {0} in XAML.".F(parameter)); } 

where Throw is defined as:

public static T Throw<T>(this T ignored, string message) {     throw new Exception(message); } 

I know that's not idiomatic C#. However, would you be able at understand it at first or second glance? Or did I stray too far?

like image 421
user76035 Avatar asked Feb 23 '10 17:02

user76035


People also ask

Is it illegible or legible?

This shows grade level based on the word's complexity. not legible; impossible or hard to read or decipher because of poor handwriting, faded print, etc.: This letter is completely illegible.

What is a word for not legible?

Definition of illegible : not legible : indecipherable illegible writing.

Is it readable or legible?

Legibility is an informal measure of how easy it is to distinguish one letter from another in a particular typeface. Readability is about the reader – the ease with which a reader can successfully decipher, process, and make meaning of the text read.

What makes legible type?

"The legibility of a typeface is related to the characteristics inherent in its design … which relate to the ability to distinguish one letter from the other." Aspects of type design that affect legibility include "x-height, character shapes, stroke contrast, the size of its counters, serifs or lack thereof, and weight ...


2 Answers

Why not use a switch? I think it's way more readable.

private bool CanExecuteAdd(string parameter) {     if (Script == null)         return false;      switch (parameter) {         case "Step":             return true;         case "Element":             return ElementSelectedInLibrary != null && SelectedStep != null;         case "Choice":             return SelectedElement != null;         case "Jump":             return SelectedStep != null;         case "Conditional jump":             return false;         default:             throw new Exception(string.Format("Unknown Add parameter {0} in XAML.", parameter));     } } 
like image 92
Fede Avatar answered Sep 22 '22 06:09

Fede


I've used this sort of code in Java a fair amount. I don't like the false.Throw bit, but having multiple conditionals like this (particularly formatted this way) is fine in my view.

It's slightly strange the very first time you see it, but after that it's just a handy pattern to know about.

One alternative to using false.Throw here would be something like this:

bool? ret = this.Script == null ? false     : parameter == "Step" ? true     : parameter == "Element" ? this.ElementSelectedInLibrary != null && this.SelectedStep != null     : parameter == "Choice" ? this.SelectedElement != null     : parameter == "Jump" ? this.SelectedStep != null     : parameter == "Conditional jump" ? false     : null;  if (ret == null) {     throw new ArgumentException(        string.Format("Unknown Add parameter {0} in XAML.", parameter); } return ret.Value; 

EDIT: Actually, in this case I wouldn't use either if/else or this pattern... I'd use switch/case. This can be very compact if you want:

if (this.Script == null) {     return false; } switch (parameter) {     case "Step": return true;     case "Element": return this.ElementSelectedInLibrary != null && this.SelectedStep != null;     case "Choice": return this.SelectedElement != null;     case "Jump": return this.SelectedStep != null;     default: throw new ArgumentException(         string.Format("Unknown Add parameter {0} in XAML.", parameter); } 
like image 27
Jon Skeet Avatar answered Sep 20 '22 06:09

Jon Skeet