- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Well, whenever I have to check if a variable equals 8, I call the variable that I'm checking against D.
<pre>If 8==D Then...</pre>
Admin
Boy, given that I can't use HTML in these comments, I sure look like a boner.
Admin
An old friend of mine used "gac" for his loop counters. "gac", of course, stood for General All-purpose Counter.
Admin
Nice thing about this code is that you get an initialized array of 8 empty strings.. saying string[8] array; gets you 8 null strings. Different thing.
You could always add a Debug.Assert(args.Length == 8); line to make it obvious that you're expecting the size to be 8.. :)
Admin
I don't think there's any guarantee as to how many strings the tmpEntry array will contain. It will be at least 9 -- one for the original contents of "entry", plus the 8 created by the commas.
It could conceivably be many more, if the "entry" variable itself contains commas.
Admin
Saying string[] array = { "", "", ""...} will get you an array of empty strings, without the overhead of splitting a string.
Admin
One possible alternative: "entry" contains a csv string that has up to 8 elements.
By concatenating ",,,,,,,," to "entry", you guarantee that args.Length is at least 8.
So IMHO, this is actually a clever, if inefficient way of parsing a variable length CSV input.
Admin
Larry Osterman,
Yes, but whoever uses this “art” is clearly asking for a test case with a comma delimiting fields, another test case with a comma /within/ a quoted field, and a third test case with both. Something tells me it’s gonna fail some of them.
foo,42
"bar,baz"
quux,"corge,xyzzy"
Admin
Oh, and by the way, Microsoft Excel, when instructed to save a worksheet to a CSV (comma-separated values) file, instead creates, so to say, a LOCALE_SLIST-separated values file. Which makes reliable CSV processing a pain because a file might have come from another machine, or be created by another user, whose list separator is different from ours.
Admin
You're right Centaur. The code above's no way to parse CSV. But maybe the person writing the code knew something about his data? It's no excuse for a lack of robustness, but...
But the code as posted IS potentially a legitimate solution to a valid problem. It's not just a stupid way of getting an array of 8 empty strings as was postulated in the original article.
Admin
private string AddToListArray()
{
return new String();
}
private string[] ArrayListToStringArray()
{
ArrayList ListArray = new ArrayList();
for (int i = -1 + 1; i < 2 * 2 * 2; i++)
{
ListArray.Add(AddToListArray());
}
return ArrayListToStringArray.ToArray(string.Empty.GetType()) as string[];
}
private void foo()
{
string[] StringArrayFromArrayList = ArrayListToStringArray();
}
// Artful.
Admin
Damn. Got caught up in my own stupidity.
private string AddToListArray()
{
return new String();
}
private string[] ArrayListToStringArray()
{
ArrayList ListArray = new ArrayList();
for (int i = -1 + 1; i < 2 * 2 * 2; i++)
{
ListArray.Add(AddToListArray());
}
return ListArray.ToArray(string.Empty.GetType()) as string[];
}
private void foo()
{
string[] StringArrayFromArrayList = ArrayListToStringArray();
}
Hey maybe MSDN can add an Obfuscated C# Contest like they do (did?) in the C++ Users Journal!
Admin
How about a Rube Goldberg C# contest instead. "Build a program that does something simple in the most steps without repeating a step."
Admin
I've actually seen code like this in our codebase. If you have your own report definition language for a report-driven application, this kind of code snippet can actually be useful. For instance, in your RDL a report element can have 5 parameters, you might want the actual report element definition to define only the parameters used.
For instance, the element might only use the first 2 parameters, or no parameters at all. In that case, the code will "default" all missing parameters. This way, you don't have code like "if the user only specified 2 parameters, then do this, if there was just 1 parameter, do this, etc.". Clever when it is applied correctly. (And no, I'm not the author of the code in our code base). The surprising part to me is that in the context of a report definition language, the code made perfect sense and is actually quite understandable and maintainable.
Admin
I should add that the codebase containing this report code is a legacy codebase and is written in classic ASP.
Admin
The first few comments that ridiculed this piece of code was unwarranted as the code was taken out of context. I would need more information before I criticise this code.
Admin
Jason, why not use a standard recursive-descent parsing tree (bison, flex-style?)
Admin
?? ??? ?? ???!
Admin
I've regularly seen
SomeFunction(someArray)
'no return because that would make too much sense. End Function
mind you this is from people who have mid-senior titles...