PC Review


Reply
Thread Tools Rate Thread

C# to the fullest: readable code

 
 
Jeroen
Guest
Posts: n/a
 
      28th Nov 2007
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
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      28th Nov 2007
On Nov 28, 12:32 pm, Jeroen <mercu...@gmail.com> wrote:
> 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
 
Reply With Quote
 
Jeroen
Guest
Posts: n/a
 
      28th Nov 2007
> 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 .


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


D'oh!


Thanks,
Jeroen
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      28th Nov 2007
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 .


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
 
Reply With Quote
 
Jeroen
Guest
Posts: n/a
 
      28th Nov 2007
Jon,

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

-Jeroen
 
Reply With Quote
 
Mythran
Guest
Posts: n/a
 
      28th Nov 2007


"Jeroen" <(E-Mail Removed)> wrote in message
news:574bfa7b-efdf-4d56-9a15-(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HOW TO HAVE TEXT THAT IS NOT READABLE BE READABLE?? JOHN Microsoft Word New Users 6 23rd Apr 2009 05:32 PM
Using the PC to it's Fullest in my Home =?Utf-8?B?cmpzbWl0aDYwOA==?= Windows XP General 2 30th May 2006 06:45 AM
Readable web GUI............ ? plun Spyware Announcements 3 17th Jun 2005 07:32 PM
I NEED TO RECONVERT A NON READABLE WORD DOC TO READABLE DOC. =?Utf-8?B?SklNIFRSQUNZ?= Microsoft Word New Users 2 13th Dec 2004 06:30 AM
desk top screen isnt at its fullest! =?Utf-8?B?c3RhcmZl?= Windows XP General 1 8th Apr 2004 10:04 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:38 AM.