tags:

views:

69

answers:

2

While working in a project today, I came across the following code:

pcShowByCategory.Controls.Add(new LiteralControl("<div id='lblDivP'>"));
pcShowByCategory.Controls.Add(new LiteralControl("<table width=100%><tr><td colspan='2' align ='left'>"));
pcShowByCategory.Controls.Add(lblTitle);
pcShowByCategory.Controls.Add(new LiteralControl("</br>"));
pcShowByCategory.Controls.Add(new LiteralControl("</br>"));
pcShowByCategory.Controls.Add(new LiteralControl("</td></tr><tr><td colspan='2'>"));
pcShowByCategory.Controls.Add(lbltitle1);
pcShowByCategory.Controls.Add(new LiteralControl("</br>"));
pcShowByCategory.Controls.Add(new LiteralControl("</td></tr><tr><td colspan='2'>"));

My initial thought was: why in the world didn't they do this in one or two lines instead of creating so many new LiteralControls. My question is - is this sloppy and wasteful of memory, and should have been consolidated to one or two instantiations of LiteralControl, or is this not a big deal?

A: 

I could imagine that this is for a reason: If you create only one instance of LiteralControl for a particular value, you might only be able to add this object to the collection once, depending on the Controls container it goes into. Some collections do not allow the same, unique object to be in it twice or more; therefore the need to create many unique instances, even if some of them have the same value:

pcShowByCategory.Controls.Add(new LiteralControl("</br>"));
pcShowByCategory.Controls.Add(new LiteralControl("</br>"));

In any case, that code doesn't look particularly nice to my eyes.

stakx
Yes, I would think that adding all of the HTML code on a single line is fine, as the browser doesn't care if it is nicely spaced. This is used in a sharepoint site where all of the code is autogenerated anyway, so why try and make 1% of it look good?
Ron
+1  A: 

if they are all hard coded string then someone should be fired.

The bare minimum 10 second refactor...

pcShowByCategory.Controls.Add(new LiteralControl("<div id='lblDivP'><table width=100%><tr><td colspan='2' align ='left'>"));
pcShowByCategory.Controls.Add(lblTitle);
pcShowByCategory.Controls.Add(new LiteralControl("</br></br></td></tr><tr><td colspan='2'>"));
pcShowByCategory.Controls.Add(lbltitle1);
pcShowByCategory.Controls.Add(new LiteralControl("</br></td></tr><tr><td colspan='2'>"));

...is still pretty rubbish but not quite so blood curdling.

Still screening for a rethink though.

runrunraygun
yes, that is precisely what I was thinking - but what of resource usage? is this a bad practice just because of sloppy coding technique, or a bad practice because of potential real-world performance loss (if this type of poor coding is used everywhere)?
Ron
@Ron: the performance-impact of sloppy code is hard to determine by looking solely at the raw code itself. The compiler is quite good at optimizing performance and as a result it's often better to write readable code than to try to hand-optimize relatively simple coding structures. This example falls under the "needlessly sloppy" category though--but in general you can't jump to conslusions with evidence of the performance impact--that's just premature optimization.
STW