pkarsh (1) [Avatar] Offline
#1
On page 19 of this book there is the following sample code:

var groups =
from word in words
orderby word ascending
group word by word.Length into lengthGroups

I personally have a problem with variable names that resemble keywords. In this case the variable name "groups" looks like it could have a relationship to what appears to be the keyword "group" . FWIW I also don't like variable names that differ only in casing but that's another story. For idiots like me it's easier if it's clear what's a variable and what variables are distinct from each other.

A more egregious example of this follows in the code that reads:

group word by word.Length into lengthGroups
orderby lengthGroups.Key descending
select new { Length = lengthGroups.Key, Words = lengthGroups };

which is followed by the code on the next page:

foreach (var group in groups)
{
Console.WriteLine("Words of length "+group.Length);
foreach (string word in group.Words)
Console.WriteLine(" "+word);
}
}
}

In the text "group.Length" I found by experimentation that "Length" was actually a user-defined property of "group" but a newbie could easily think that "Length" is a keyword in this context.

I rewrote the above code as:

var allwordsgrouped = from aWord in mHelloWorldString
group aWord by aWord.Length into GroupByLength
orderby GroupByLength.Key descending
select new { WordLength = GroupByLength.Key, Words = GroupByLength };

foreach (var aWordGrouped in allwordsgrouped) {
Console.WriteLine("Words of length " + aWordGrouped.WordLength);
foreach (string aWord in aWordGrouped.Words) {
Console.WriteLine(aWord);
}
}



Hope this helps. Thanks for listening.
steve.eichert (22) [Avatar] Offline
#2
Re: A nit
Paul,

Thanks for the recommendation. We've been hard at work getting the final version of the book ready and have made some updates to code samples and text to make things more clear. I can't speak to whether the specific example you noted was changed, however, I'll drop Fabrice a note (he wrote the chapter you mentioned) and make sure he has a look at.

Best Regards,
Steve