Home All Groups Group Topic Archive Search About

C# to the fullest: readable code

Author
28 Nov 2007 12:32 PM
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

Author
28 Nov 2007 12:41 PM
Jon Skeet [C# MVP]
On Nov 28, 12:32 pm, Jeroen <mercu***@gmail.com> wrote:
Show quote
> 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
Author
28 Nov 2007 12:56 PM
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
Author
28 Nov 2007 1:14 PM
Jon Skeet [C# MVP]
On Nov 28, 12:56 pm, Jeroen <mercu***@gmail.com> wrote:
> > 1) Use a StringBuilder
>
> 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
Author
28 Nov 2007 1:41 PM
Jeroen
Jon,

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

-Jeroen
Author
28 Nov 2007 5:38 PM
Mythran
"Jeroen" <mercu***@gmail.com> wrote in message
news:574bfa7b-efdf-4d56-9a15-12cdf4042881@d27g2000prf.googlegroups.com...
> 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

AddThis Social Bookmark Button