Click here to Skip to main content
15,886,860 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.

 
QuestionRe: Don't they know what an array is? Pin
harold aptroot17-Nov-09 23:17
harold aptroot17-Nov-09 23:17 
AnswerRe: Don't they know what an array is? Pin
Member 448708322-Nov-09 0:09
Member 448708322-Nov-09 0:09 
GeneralRe: Don't they know what an array is? Pin
harold aptroot22-Nov-09 0:57
harold aptroot22-Nov-09 0:57 
GeneralRe: Don't they know what an array is? Pin
phannon8622-Nov-09 22:33
professionalphannon8622-Nov-09 22:33 
GeneralRe: Don't they know what an array is? Pin
VickyC#21-Nov-09 15:16
VickyC#21-Nov-09 15:16 
GeneralRe: Don't they know what an array is? Pin
Member 448708322-Nov-09 0:11
Member 448708322-Nov-09 0:11 
GeneralRe: Don't they know what an array is? Pin
Shameel24-Nov-09 23:42
professionalShameel24-Nov-09 23:42 
GeneralRe: Don't they know what an array is? Pin
dojohansen25-Nov-09 2:21
dojohansen25-Nov-09 2:21 
I feel your pain, and yet I suspect you feel only some of mine. In the project I am working on this would no doubt have been implemented as follows:

string foobar(string xml)
{
   XmlDocument doc = new XmlDocument();
   doc.LoadXml(xml);

   // ... do something to doc

   // pick out some part of the doc that another function processes...
   string sXmlSelection = xml.SelectSingleNode("//xmlSelection").OuterXml;
   sXmlSelection = foo(sXmlSelection);
   string sXmlRestriction = xml.SelectSingleNode("//xmlRestriction").OuterXml;
   sXmlRestriction = foo(sXmlRestriction);

   ...

   return doc.OuterXml;
}

string foo(string xml)
{
   XmlDocument doc = new XmlDocument();
   doc.LoadXml(xml);
   // ... do something to doc
   // pick out some part of the doc that another function processes...
   string s = xml.SelectSingleNode(...).OuterXml;
   s = bar(s);

   ...

   return doc.OuterXml;
}

string bar(string xml)
{
   XmlDocument doc = new XmlDocument();
   doc.LoadXml(xml);

   // ... do something to doc

   return doc.OuterXml;
}

void main()
{
   string xmlSelection = "<data><record opt='new'>";
   xmlSelection += "<elem1>" + getElem() + </elem1>";
   xmlSelection += "<elem2>" + getElem() + </elem2>";
   xmlSelection += "<elem3>" + getElem() + </elem3>";
   xmlSelection += "<elem4>" + getElem() + </elem4>";
   xmlSelection += "</record></data>");

   string xmlRestriction = "<data/>";
   
   string xmlData = <data><xmlSelection>" + xmlSelection + "</xmlSelection>";
   xmlData += "<xmlRestriction>" + xmlRestriction + "</xmlRestriction></data>";

   foobar(xmlData);
}


I swear I've seen call stacks where eight methods are all calling one another with strings and returning strings, all of them working on xml, even though in most cases the only information used by the methods is the innertext of particular nodes. Every method parses the string to build an xml document, finds the real parameters to the method as text - never checking if nodes exist and thus ensuring a meaningless NullReferenceException in the event the poor person trying to use this "business logic" fails to pass the correct (and undocumented) xml to a method - then does some work such as selecting something from a database, stuffs the result into the xml document somewhere, and returns the OuterXml.

Apart from this leading to code that spends virtually all it's time parsing xml strings and rendering documents back to such strings it is also incredibly wasteful of memory. And when what could have been int arrays grow large it leads to additional problems because of large objects (the strings are of course continous in memory, making it harder for the garbage collector to move them around).

If anyone has any idea where this idea that string is the perfect data structure for absolutely anything comes from, I would love to know. I have never been able to understand it.
JokeRe: Don't they know what an array is? Pin
PIEBALDconsult25-Nov-09 16:40
mvePIEBALDconsult25-Nov-09 16:40 
GeneralRe: Don't they know what an array is? Pin
supercat93-Dec-09 10:03
supercat93-Dec-09 10:03 
GeneralRe: Don't they know what an array is? Pin
dojohansen4-Dec-09 1:02
dojohansen4-Dec-09 1:02 
GeneralRe: Don't they know what an array is? Pin
Timothy Byrd23-Dec-09 12:51
Timothy Byrd23-Dec-09 12:51 
GeneralWhy? Why?!! WHY!!!! Pin
Jeremy Hutchinson11-Nov-09 9:19
professionalJeremy Hutchinson11-Nov-09 9:19 
GeneralRe: Why? Why?!! WHY!!!! Pin
Sundance Kid11-Nov-09 18:49
Sundance Kid11-Nov-09 18:49 
GeneralRe: Why? Why?!! WHY!!!! Pin
Corinna John20-Nov-09 0:38
Corinna John20-Nov-09 0:38 
GeneralRe: Why? Why?!! WHY!!!! Pin
VickyC#21-Nov-09 15:21
VickyC#21-Nov-09 15:21 
GeneralRe: Why? Why?!! WHY!!!! Pin
Jeremy Hutchinson24-Nov-09 7:27
professionalJeremy Hutchinson24-Nov-09 7:27 
GeneralRe: Why? Why?!! WHY!!!! Pin
Mycroft Holmes25-Nov-09 18:32
professionalMycroft Holmes25-Nov-09 18:32 
GeneralThis is a pleasure to read. Pin
Wes Jones5-Nov-09 5:56
Wes Jones5-Nov-09 5:56 
GeneralRe: This is a pleasure to read. Pin
Nagy Vilmos6-Nov-09 4:09
professionalNagy Vilmos6-Nov-09 4:09 
GeneralRe: This is a pleasure to read. Pin
David Skelly6-Nov-09 5:00
David Skelly6-Nov-09 5:00 
GeneralRe: This is a pleasure to read. Pin
Wes Jones6-Nov-09 5:28
Wes Jones6-Nov-09 5:28 
GeneralRe: This is a pleasure to read. Pin
Jeremy Hutchinson11-Nov-09 9:06
professionalJeremy Hutchinson11-Nov-09 9:06 
GeneralRe: This is a pleasure to read. Pin
Wes Jones12-Nov-09 7:45
Wes Jones12-Nov-09 7:45 
QuestionRe: This is a pleasure to read. Pin
Lutosław16-Nov-09 23:14
Lutosław16-Nov-09 23:14 

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.