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?
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.
Definition of illegible : not legible : indecipherable illegible writing.
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.
"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 ...
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)); } }
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With