C# to the fullest: readable code

J

Jeroen

Hi,

I've found that C# 2 has several non-basic-programming-features turn
out to be a great help in making code readable and typesafe, most
notable the Generics stuff. Consequently, when I was creating the code
below, I figured there must be an easier and above all more readable
way to do this:

/*---------------------------------------*/
private string ImplodeMyObjects (MyClass[] items)
{
string result = string.Empty;
foreach (MyClass item in items)
result += item.ToString();
return result;
}

private void InterestingFunction()
{
MyClass[] items = GetFromFoo();
string coolstring = ImplodeMyObjects(items);
}
/*---------------------------------------*/

It seems that C# 3.5 will provide even cooler stuff to do the above,
but even in C# 2 there should be a nicer way to do this? Any comments
and suggestions on how to handle this kind of stuff would be much
appreciated. Please indicate for which version any suggestions would
be.

Thanks.

-Jeroen
 
J

Jon Skeet [C# MVP]

I've found that C# 2 has several non-basic-programming-features turn
out to be a great help in making code readable and typesafe, most
notable the Generics stuff. Consequently, when I was creating the code
below, I figured there must be an easier and above all more readable
way to do this:

/*---------------------------------------*/
private string ImplodeMyObjects (MyClass[] items)
{
string result = string.Empty;
foreach (MyClass item in items)
result += item.ToString();
return result;

}

Well, a few suggestions:
1) Use a StringBuilder
2) Make the method generic
3) Make the parameter IEnumerable
4) Make it static

static string ImplodeMyObjects<T>(IEnumerable<T> data)
{
StringBuilder builder = new StringBuilder();
foreach (T item in data)
{
builder.Append(item);
}
return builder.ToString();
}

I think that's pretty readable on its own, isn't it?

With C# 3 (not 3.5 - that's the version of the framework) you could
make it an extension method just by changing the parameter list to
(this IEnumerable<T> data) so long as it's in a static class. That
would let you do:

private void InterestingFunction()
{
string combined = GetFromFoo().ImplodeMyObjects();
}

Is this any help?

Jon
 
J

Jeroen

Is this any help?

Yes Jon, thanks, this is a good start for me. I have one follow-up
question though:

1) Use a StringBuilder

Why to use this instead of simply attaching things one at a time to a
string?


Also, I'm getting more and more curious whether it would also be
possible to do without a function at all, and just do this on one
line, though that would probably not improve readability :D.

With C# 3 (not 3.5 - that's the version of the framework)

D'oh!


Thanks,
Jeroen
 
J

Jon Skeet [C# MVP]

Why to use this instead of simply attaching things one at a time to a
string?

See http://pobox.com/~skeet/csharp/stringbuilder.html
Also, I'm getting more and more curious whether it would also be
possible to do without a function at all, and just do this on one
line, though that would probably not improve readability :D.

Well, with C# 3 it *is* all in one line, just with an extra method.
That's likely to be a lot nicer than anything that's built in. (I'm
sure it's possible with LINQ and the Aggregate method, but you really
don't want to see what it would involve.)

Jon
 
J

Jeroen

Jon,

Thanks a bunch for your helpful responses, at such a short notice
even!

-Jeroen
 
M

Mythran

Jeroen said:
Jon,

Thanks a bunch for your helpful responses, at such a short notice
even!

-Jeroen

Not to put anyone else down since lots of people help in this community, and
I'm not the first to say this, but Jon is one of the most contributing
members to this community and jumps all over questions he's able to answer.
He's a very good guy when it comes to this community (thought I'd mention
that since I don't know him in person but have spoken to him via NG quite
often). Also used his site more than a few times to help clarify a few
things for myself....

Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top