>If you find it harder to read, simply don't use it. ;-)
>>
That's one of the poorest excuses I've heard to not use something.
Agreed. My goal was simply to show that the Array.ConvertAll<TInput, TOutput>
method exists and can be used for simple array conversion operations. The
truth is, I think that the original method is difficult to read and doesn't
really exhibit good design. This helped the anonymous method to overcomplicate
things even further because anonymous methods *should* be short. For reference,
here it is again:
private void ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
uint[] IntCard = new uint[CardsToBeSortedArray.Length];
foreach (string Card in CardsToBeSortedArray)
{
UInt32.TryParse(Card, out IntCard);
}
}
And a working, sort-of-complete version:
private uint[] ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
uint[] IntCard = new uint[CardsToBeSortedArray.Length];
for (int i = 0; i < CardsToBeSortedArray.Length; i++)
UInt32.TryParse(CardsToBeSortedArray[i], out IntCard[i]);
return IntCard;
}
IMO, using UInt32.TryParse this way makes the method far more difficult to
read. It's almost as if UInt32.TryParse is being shoe-horned in. Because
of the way in which UInt32.TryParse is designed, a mental jump has to be
made because the assignment actually happens at the end of the method call.
Also, you must to have the intimate knowledge that UInt32.TryParse will set
IntCard[i] to zero if it fails. So, the code in this method is semantically
coupled to UInt32.TryParse and that makes a very simple operation much harder
to read.
Here's one way to improve readability:
private uint ParseCard(string card)
{
uint result;
if (!UInt32.TryParse(card, out result))
result = 0;
return result;
}
private uint[] ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
uint[] IntCard = new uint[CardsToBeSortedArray.Length];
for (int i = 0; i < CardsToBeSortedArray.Length; i++)
IntCard[i] = ParseCard(CardsToBeSortedArray[i]);
return IntCard;
}
The call to UInt32.TryParse has been extracted to a more readable method
called "ParseCard". In addition, the semantic coupling to UInt32.TryParse
has been removed because the code no longer assumes that zero is returned
on failure. So, if Microsoft does anything insane or if the code runs on
a different .NET platform, we're protected against changes there.
And now, Array.ConvertAll can be used to reduce the code:
private uint[] ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
return Array.ConvertAll<string, uint>(CardsToBeSortedArray, delegate(string
card) { return ParseCard(card); });
}
Or, if you prefer not using an anonymous method, you can can simple specify
the method and reduce the code further:
private uint[] ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
return Array.ConvertAll<string, uint>(CardsToBeSortedArray, ParseCard);
}
I actually dislike that code a bit because it isn't clear what is going on.
It's harder to see that ParseCard is a method that is going to be called
with a specific parameter and its return value used. So, I prefer the clarity
of an anonymous method -- even though I detest the "delegate" keyword it
requires. That said, lambda expressions realy excite me because I can improve
the readability of the Array.ConvertAll call in some cool ways. In C# 3.0,
if a lambda expression is used, the compiler has enough information to figure
out what the generic arguments of Array.ConvertAll should be. So, the code
becomes this:
private uint[] ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
return Array.ConvertAll(CardsToBeSortedArray, card =ParseCard(card));
}
I like this most of all because it gives me the readable qualities that I
liked about the anonymous method example and makes it more succinct at the
same time. But, then again, I've been playing with lambda expressions a great
deal so I'm pretty comfortable with them.
Best Regards,
Dustin Campbell
Developer Express Inc.