Click here to Skip to main content
15,911,896 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: Horrible enough for you? Pin
CPallini1-Oct-08 5:34
mveCPallini1-Oct-08 5:34 
GeneralRe: Horrible enough for you? Pin
PIEBALDconsult1-Oct-08 8:51
mvePIEBALDconsult1-Oct-08 8:51 
GeneralRe: Horrible enough for you? Pin
dojohansen1-Oct-08 21:29
dojohansen1-Oct-08 21:29 
JokeRe: Horrible enough for you? Pin
cpkilekofp2-Oct-08 10:04
cpkilekofp2-Oct-08 10:04 
GeneralRe: Horrible enough for you? Pin
VentsyV1-Oct-08 11:46
VentsyV1-Oct-08 11:46 
GeneralRe: Horrible enough for you? Pin
Lutosław1-Oct-08 12:04
Lutosław1-Oct-08 12:04 
GeneralRe: Horrible enough for you? Pin
Paul Conrad1-Oct-08 18:58
professionalPaul Conrad1-Oct-08 18:58 
GeneralRe: Horrible enough for you? Pin
dojohansen1-Oct-08 22:13
dojohansen1-Oct-08 22:13 
He was trying to copy a subset of xml data from one document to another (how this can be useful beats me unless you unload the document containing the superset of data, but anyway that's what he tried to do) but the data would not have the same nodes in all cases. Apparently upon discovering this he figured he'd just wrap the code copying each node into a try-catch so it continues if a node doesn't exist.

This is a lazy approach and a coding horror in my book. First, the cost of catching exceptions instead of checking if nodes exist before trying to clone them is *huge*. Second, in most cases the try block can fail for other reasons than the one which "is ok" and means you should just go on. This project is plagued by that particular problem, people catch an exception because they want the program to "keep working" but of course all it does is make it fail in strange ways while leaving no useful debugging information. It's worse when they do this server-side since the client-side exceptions aren't so rich on information anyway but the principle is the same.

My real gripe however is the code redundancy. If you intend to perform the same operation 20 times over you should use the same implementation 20 times, so it's possible to change the darn thing without getting bored to death.

I also think that if the operation is of a fairly generic nature, say, copying a subset of xml nodes from one document to another, you should separate the logic from any particular bits of the systems such as, say, a specific web page in a complex application. The guy is now Chef de projet développements (not for my team thankfully) and continues to wreak havoc at undiminished speed.

An example of what he might have written instead:

function copyChildNodes(srcNode, destNode, nodeList, ignoreMissingNodes)
{
   for (var i=0; i < nodeList.length; i++)
   {
      var node = srcNode.selectSingleNode(nodeList[i]);
      if (!node)
      {
         if (ignoreMissingNodes) 
            continue;
         else 
            throw {message: "No child node with name '" + nodeList[i] + "' exists in the source node."};
      }
      destNode.appendChild(node.cloneNode(true));
   }
}


Put that in one of our several script files intended for code a little bit more general than the page-specfic stuff and put this in the page (instead of the twenty lines doing the same thing):

var nodes = ["block_name", "block_type", "title", "list_id", "you", "get", "the", "point"];
copyChildNodes(oXMLBlock, oXMLRecord, nodes, true);


Surely you can see how this second approach allows a long-term maintenance of the code that is completely incomparable to the copy-paste approach. I like to call it the "cut-and-paste" approach actually, because in practice it often starts with the realization that we're writing code in the wrong place, that a lot of what we're coding is not specific to the task and should be refactored to allow us to reuse it. This refactoring usually starts with cutting out part of the code and moving it somewhere else, then perhaps cleaning it up a bit or parameterizing some aspect of the behavior, such as "ignoreMissingNodes" in the above example. Reuse is extremely important, not just to save time (copy-paste achieves that, at first development, nearly as well), but to fix bugs and improve the QUALITY of the code base over time.
GeneralRe: Horrible enough for you? Pin
Dan Neely2-Oct-08 2:25
Dan Neely2-Oct-08 2:25 
GeneralRe: Horrible enough for you? Pin
rcollina2-Oct-08 1:09
rcollina2-Oct-08 1:09 
GeneralRe: Horrible enough for you? Pin
Tom12-Oct-08 8:27
Tom12-Oct-08 8:27 
GeneralAnd Another One PinPopular
Rick York29-Sep-08 12:22
mveRick York29-Sep-08 12:22 
QuestionRe: And Another One Pin
CPallini29-Sep-08 21:44
mveCPallini29-Sep-08 21:44 
GeneralRe: And Another One Pin
Graham Bradshaw30-Sep-08 2:06
Graham Bradshaw30-Sep-08 2:06 
GeneralRe: And Another One Pin
Mike Dimmick30-Sep-08 5:49
Mike Dimmick30-Sep-08 5:49 
GeneralRe: And Another One Pin
Rick York30-Sep-08 7:09
mveRick York30-Sep-08 7:09 
GeneralAnother Horror PinPopular
Narotham Babu Kalluri26-Sep-08 3:39
Narotham Babu Kalluri26-Sep-08 3:39 
GeneralRe: Another Horror Pin
adamsappel26-Sep-08 3:52
adamsappel26-Sep-08 3:52 
GeneralRe: Another Horror Pin
Paul Conrad26-Sep-08 6:05
professionalPaul Conrad26-Sep-08 6:05 
GeneralRe: Another Horror Pin
CARPETBURNER29-Sep-08 8:59
CARPETBURNER29-Sep-08 8:59 
QuestionRe: Another Horror Pin
CPallini26-Sep-08 4:07
mveCPallini26-Sep-08 4:07 
AnswerRe: Another Horror Pin
Narotham Babu Kalluri26-Sep-08 5:01
Narotham Babu Kalluri26-Sep-08 5:01 
GeneralRe: Another Horror Pin
Paul Conrad26-Sep-08 6:06
professionalPaul Conrad26-Sep-08 6:06 
JokeRe: Another Horror Pin
Single Step Debugger26-Sep-08 5:14
Single Step Debugger26-Sep-08 5:14 
GeneralRe: Another Horror Pin
Paul Conrad26-Sep-08 6:04
professionalPaul Conrad26-Sep-08 6:04 

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.