//Comment This Out

Tuesday, March 8, 2005

Poorly Written Code: Part II

I'd thought I'd continue on the theme of my last post and give a few more examples of poorly written code. The following is what I consider a textbook case on how not to program of which I had the misfortune of dealing with last year. In the application I was dealing with, there is a search utility that searchs for accounts based on various criteria (e.g. name, address, postal code, etc.). When the search would be invoked it would call a single method over 1200 lines long!This method was a virtual rabbit's warren of nested if...else code blocks. Within this mess, it would instantiate objects from one of two basic classes. One of these classes had 19 different constructors, the other one had 12. And yes, it used every one of those. It turns out that every time someone had added another search parameter, they added another set of constructors to those classes to accomodate the new parameter (since these classes were originally designed in such a way that the only way to pass in search parameters was through a constructor). By the time I finished rewriting the mess, I had reduced the 1200 line method down to about 150 lines of code and reduced the number of constructors in both classes down to 2. One of my co-workers told me a story last week that at a former job, he was writing code in C. In the code he was working on, he found someone had used a goto statement. This is what he found: if (some argument) { // do something goto a_label: } else { a_label: // do something }


Post a Comment

<< Home