[dba-VB] Getting TextReader from strings...

Shamil Salakhetdinov shamil at smsconsulting.spb.ru
Thu Jun 26 10:50:33 CDT 2008


Hello Charlotte,

Thank you for your opinion.

You're surely a Geek also as Gustav is. :)

--
Shamil

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Charlotte Foust
Sent: Thursday, June 26, 2008 7:06 PM
To: Discussion concerning Visual Basic and related programming issues.
Subject: Re: [dba-VB] Getting TextReader from strings...

I don't see what the point of an array would be ... Unless you were
passing one into the routine.  Populating an array in the code wouldn't
be any simpler or any geekier, IMO.  And while I personally tend to use
the MMW approach in my own code, I find the geekier version *easier* to
read in this case, maybe because of all the extra punctuation in C#.  If
I had to debug it, I'd have to deconstruct it, but I'm becoming more
comfortable with the geek approach.

Charlotte Foust 

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Gustav Brock
Sent: Thursday, June 26, 2008 1:50 AM
To: dba-vb at databaseadvisors.com
Subject: Re: [dba-VB] Getting TextReader from strings...

Hi Shamil

I prefer the MMW. Much easier to read. You can read that code in one go.
With the MGW you most often have to locate the "core", here
GetBytes(sb.ToString(), then read backwards to find out what is done,
and then forward once again. And you obtain no points for using a
smaller count of characters.

Also, it is much easier to modify. Say you later found out that you had
to verify the count of bytes:

    System.Text.ASCIIEncoding ascii = new ASCIIEncoding();
    byte[] bytes = ascii.GetBytes(sb.ToString());
    MemoryStream ms = new MemoryStream(bytes); 

Just insert:
    byte[] bytes = ascii.GetBytes(sb.ToString());
    if (bytes.Length > something) ... do something

As for ILDASM, I don't have it but I doubt the difference in code will
be of any significance.

For a geeker method, I guess you could replace the multiple
AppendBuilder calls with an array somehow but it would just raise the
level of unreadability.

/gustav


>>> shamil at smsconsulting.spb.ru 26-06-2008 09:32 >>>
Hi All,

Below is a useful code written two ways:

1) "mere mortals' way" (MMW)
2) "geek way" (geeks will agree hopefully - in fact there exists even
"geekier" version of this code (contest! contest! - for Friday :)) - I
used "moderate geek way"(MGW) mode)

Questions:

1) Which way you prefer? (I personally prefer "mere mortals' way".
2) Anybody who is in love with ILDASM could you please check does the
two version of code generate the same IL in code optimization mode or
even always?

Thank you.

--
Shamil

P.S. Subject code:

//1. MMW

private TextReader Test1()
{
    StringBuilder sb = new StringBuilder();
    sb.AppendLine("Line1");
    sb.AppendLine("Line2");
    sb.AppendLine("Line3");            
    System.Text.ASCIIEncoding ascii = new ASCIIEncoding();
    byte[] bytes = ascii.GetBytes(sb.ToString());
    MemoryStream ms = new MemoryStream(bytes); 
    return new StreamReader(ms);
}

//2. MGW

private TextReader Test2()
{
    StringBuilder sb = new StringBuilder();
    sb.AppendLine("Line1");
    sb.AppendLine("Line2");
    sb.AppendLine("Line3");            
    return 
        new StreamReader( 
        new MemoryStream((
        new ASCIIEncoding(
        )).GetBytes(sb.ToString())));
}


_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com 



_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com


_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com




More information about the dba-VB mailing list