I just wrote the following function:
public string Ebnf {
get {
var props = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
var ruleProps = from p in props where p.PropertyType.IsSubclassOf(typeof(ARule)) select p;
var rules = from p in ruleProps select (ARule)p.GetValue(this, null);
var ebnfs = from r in rules select r.Name + " = " + r.Ebnf + ".";
return string.Join("\n", ebnfs.ToArray());
}
}
I started wondering if Linq actually saved me space, or whether I was using Linq just for the sake of it:
public string EbnfNonLinq {
get {
var ebnfs = new List<string>();
var props = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var p in props) {
if (p.PropertyType.IsSubclassOf(typeof(ARule))) {
var r = (ARule)p.GetValue(this, null);
ebnfs.Add(r.Name + " = " + r.Ebnf + ".");
}
}
return string.Join("\n", ebnfs.ToArray());
}
}
7 lines of code vs. 5: it's a savings. But I wonder if the density of the first function is too much. (This is not performance critical code, so I am not concerned about that.)
Which do you think is prettier, more maintainable, comprehensible, better?