Shamil Salakhetdinov
shamil at smsconsulting.spb.ru
Thu Jun 26 10:50:33 CDT 2008
Hi Gustav, You're a Geek! :) I prefer "mere mortals' way". I think I see some advantages of MMW against MGW but I'd better stay quiet to not make this thread like getting into flame mode :) Yes, from aesthetic point of view I like MGW but from practical I prefer MMW... The only disadvantage would be if the code generated for MGW would be somehow considerably quicker for large strings' sets... Thank you. -- Shamil -----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()))); } _______________________________________________ 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