Click here to Skip to main content
15,897,371 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Range-Checking (not) Pin
Dalek Dave3-Nov-10 6:49
professionalDalek Dave3-Nov-10 6:49 
GeneralRe: Range-Checking (not) Pin
AspDotNetDev3-Nov-10 7:16
protectorAspDotNetDev3-Nov-10 7:16 
GeneralRe: Range-Checking (not) Pin
Rob Grainger4-Nov-10 1:52
Rob Grainger4-Nov-10 1:52 
GeneralRe: Range-Checking (not) Pin
richard_k7-Nov-10 22:02
richard_k7-Nov-10 22:02 
GeneralThe non-blocking command line parameter Pin
Bernhard Hiller1-Nov-10 22:51
Bernhard Hiller1-Nov-10 22:51 
GeneralRe: The non-blocking command line parameter Pin
fjdiewornncalwe2-Nov-10 1:24
professionalfjdiewornncalwe2-Nov-10 1:24 
GeneralRe: The non-blocking command line parameter Pin
Fueled By Decaff8-Nov-10 19:37
Fueled By Decaff8-Nov-10 19:37 
GeneralALWAYS USE A STRINGBUILDER! [modified] Pin
PIEBALDconsult1-Nov-10 17:49
mvePIEBALDconsult1-Nov-10 17:49 
Everyone knows that if you are doing a lot of string concatenation in a loop, you absolutely must use a StringBuilder for performance!

I found something like this today (the original is in VB.net). It formats some data for a fixed-width text file.

StringBuilder result = new StringBuilder() ;

foreach ( DataRow dr in datatable.Rows ) 
{
    result.AppendLine ( "".PadRight ( ' ' , 9 ) + dr [ 0 ].ToString().PadRight ( ' ' , 9 ) + "".PadRight ( ' ' , 9 ) + dr [ 1 ].ToString().PadRight ( ' ' , 9 ) ... et cetera et cetera et cetera ... the statement is nearly 800 characters long ... ) ;
}

return ( result.ToString() ) ;


I suspect that each resultant line is also nearly 800 characters long. I'm also fairly sure that the data table doesn't usually contain many rows, so percentage-wise not many concatenations are eliminated here.


As I'm new to the company, I chose the "it ain't broke, don't fix it" option. But I added comments suggesting that:

0) new string ( ' ' , 9 ) might perform better, is easier to read, and doesn't make the developer look stoooopid.
1) Proper use of a StringBuilder would definitely be a wise course of action.


Now, I would also like to ask your opinion... This code builds one big-A string in memory and passes it back where it is simply written to a file. Personally, I would pass in a stream and write to it as I go, eliminating the memory hoggage. Another option would be to use events -- raise an event for each line (or field) and the calling code can write it to the file, but I don't really like that here.

If you were writing a data to a fixed-width text file, what technique would you choose? ("Whatever the boss specifies" doesn't count.)



P.S. Don't get me started on how they generate XML... Sigh | :sigh:

modified on Tuesday, November 2, 2010 12:05 AM

GeneralRe: ALWAYS USE A STRINGBUILDER! Pin
fjdiewornncalwe2-Nov-10 1:30
professionalfjdiewornncalwe2-Nov-10 1:30 
GeneralRe: ALWAYS USE A STRINGBUILDER! PinPopular
Samuel Cragg2-Nov-10 2:21
Samuel Cragg2-Nov-10 2:21 
GeneralRe: ALWAYS USE A STRINGBUILDER! Pin
fjdiewornncalwe2-Nov-10 2:38
professionalfjdiewornncalwe2-Nov-10 2:38 
GeneralRe: ALWAYS USE A STRINGBUILDER! Pin
AspDotNetDev2-Nov-10 13:28
protectorAspDotNetDev2-Nov-10 13:28 
GeneralRe: ALWAYS USE A STRINGBUILDER! Pin
AspDotNetDev2-Nov-10 13:40
protectorAspDotNetDev2-Nov-10 13:40 
GeneralRe: ALWAYS USE A STRINGBUILDER! Pin
PIEBALDconsult2-Nov-10 15:26
mvePIEBALDconsult2-Nov-10 15:26 
GeneralRe: ALWAYS USE A STRINGBUILDER! Pin
Robert Rohde3-Nov-10 6:32
Robert Rohde3-Nov-10 6:32 
GeneralRe: ALWAYS USE A STRINGBUILDER! Pin
PIEBALDconsult3-Nov-10 16:57
mvePIEBALDconsult3-Nov-10 16:57 
GeneralRe: ALWAYS USE A STRINGBUILDER! Pin
richard_k5-Nov-10 19:18
richard_k5-Nov-10 19:18 
GeneralRe: ALWAYS USE A STRINGBUILDER! Pin
Member 968-Nov-10 6:25
Member 968-Nov-10 6:25 
GeneralRe: ALWAYS USE A STRINGBUILDER! Pin
PIEBALDconsult8-Nov-10 13:55
mvePIEBALDconsult8-Nov-10 13:55 
GeneralRe: ALWAYS USE A STRINGBUILDER! Pin
Member 968-Nov-10 16:13
Member 968-Nov-10 16:13 
GeneralRe: ALWAYS USE A STRINGBUILDER! Pin
PIEBALDconsult9-Nov-10 2:05
mvePIEBALDconsult9-Nov-10 2:05 
GeneralRange Checking Pin
AspDotNetDev31-Oct-10 12:25
protectorAspDotNetDev31-Oct-10 12:25 
GeneralRe: Range Checking Pin
fjdiewornncalwe31-Oct-10 16:02
professionalfjdiewornncalwe31-Oct-10 16:02 
GeneralRe: Range Checking Pin
AspDotNetDev1-Nov-10 6:19
protectorAspDotNetDev1-Nov-10 6:19 
GeneralRe: Range Checking Pin
fjdiewornncalwe2-Nov-10 1:33
professionalfjdiewornncalwe2-Nov-10 1:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.