Comment On Array Initializer

Some programmers feel that the code they produce is as much artwork as it is function. I guess I can agree with that, especially after viewing Anges Martin's The City at my local art museum, which reminds us that anything can be art. And with art comes creativity. So I'll bet that's what the author of this piece of code (sent in by Simon) was going for: [expand full text]
« PrevPage 1Next »

re: Array Initializer

2004-06-10 14:30 • by Jake Vinson
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>

re: Array Initializer

2004-06-10 14:31 • by Jake Vinson
Boy, given that I can't use HTML in these comments, I sure look like a boner.

re: Array Initializer

2004-06-10 14:48 • by Mike Dunn
An old friend of mine used "gac" for his loop counters. "gac", of course, stood for General All-purpose Counter.

re: Array Initializer

2004-06-10 14:53 • by Steve Tibbett
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.. :)

re: Array Initializer

2004-06-10 15:32 • by Steve Maine
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.

re: Array Initializer

2004-06-10 15:51 • by Jerry Pisk
Saying string[] array = { "", "", ""...} will get you an array of empty strings, without the overhead of splitting a string.

re: Array Initializer

2004-06-10 16:18 • by Larry Osterman
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.

re: Array Initializer

2004-06-10 23:32 • by Centaur
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"

re: Array Initializer

2004-06-10 23:42 • by Centaur
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.

re: Array Initializer

2004-06-11 11:32 • by Larry Osterman
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.

re: Array Initializer

2004-06-11 16:00 • by Christian Romney
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.

re: Array Initializer

2004-06-11 16:03 • by Christian Romney
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!

re: Array Initializer

2004-06-11 17:37 • by Jason
How about a Rube Goldberg C# contest instead. "Build a program that does something simple in the most steps without repeating a step."

re: Array Initializer

2004-06-11 18:34 • by Jason Olson
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.

re: Array Initializer

2004-06-11 18:35 • by Jason Olson
I should add that the codebase containing this report code is a legacy codebase and is written in classic ASP.

re: Array Initializer

2004-06-17 10:52 • by Hassan Voyeau
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.

re: Array Initializer

2004-07-20 11:54 • by m
Jason, why not use a standard recursive-descent parsing tree (bison, flex-style?)

Re: Array Initializer

2007-03-20 13:48 • by AKG (unregistered)
127573 in reply to 22338
?? ??? ?? ???!
« PrevPage 1Next »

Add Comment