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

Gustav Brock Gustav at cactus.dk
Thu Jun 26 03:49:53 CDT 2008


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 






More information about the dba-VB mailing list