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

Gustav Brock Gustav at cactus.dk
Thu Jun 26 11:30:15 CDT 2008


Hi Shamil

Ha, I'm glad even you can be confused!

Still, Geek or no-Geek? I like some Geek code just for fun. But I've learned the hard way to stay off for production code. As said, no one assigns you points for writing tight not-so-comprehensible code. It simply doesn't pay off. One example is that - most of us, I guess - often copy snippets of code from one project to another to reuse not as is but as a skeleton for something similar. If you have to spend several minutes just figuring out how your original code works, you are wasting your time. The real lesson is when you after many minutes still can't find out - that's when I stopped playing smart.

This is different from speed optimization. If a piece of code has to appear "Geeky" to run fast, so be it. But then you often spend so much time reaching the final code that you easily can spend the few minutes to insert decent in-line comments. I write much in-line code; it doesn't take much time, and sometimes - while you are thinking - write it down. Great value when you return a year later to adjust the code!

/gustav

>>> shamil at smsconsulting.spb.ru 26-06-2008 17:55 >>>
Hello Gustav,

Sorry in my previous e-mail I did suppose you prefer MGW but you're
preferring MMW as I do....

But anyway - you're a real Geek as well as Charlotte is :)

Thank you.

--
Shamil

P.S. Sorry again - programmed here all the day through, and it looks like
WITIRINWWWIADEMIR - "What I'm Thinking I'm Reading Is Not What Was Written
In Access-D E-Mail I'm Reading"... :)

-----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 12:50 PM
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())));
}





More information about the dba-VB mailing list