The Artima Developer Community
Sponsored Link

Java Community News
What is the Worst Code You've Ever Seen?

45 replies on 4 pages. Most recent reply: Jan 16, 2007 11:21 AM by Greg Engel

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 45 replies on 4 pages [ « | 1 2 3 4 | » ]
Bruno Thomas

Posts: 1
Nickname: bamthomas
Registered: Jan, 2007

Re: What is the Worst Code You've Ever Seen? Posted: Jan 8, 2007 2:12 AM
Reply to this message Reply
Advertisement
I've seen in a code review of a C++ class in production :


/* in an instance method */
if (this == null) {
return 0;
}


In a way, it was not harmful but it made me think the developper missed something about object...

Terje Slettebø

Posts: 205
Nickname: tslettebo
Registered: Jun, 2004

Re: What is the Worst Code You've Ever Seen? Posted: Jan 8, 2007 3:10 AM
Reply to this message Reply
Jeff Ratcliff:

> IMHO the only universal and objective criteria for bad
> code is that it doesn't fulfill its requirements. Any
> other criteria that is applied without a specific context
> is subjective.

That's a reasonable position. However, possibly unless the code is a one-off script, and in particular for code that is meant to be further developed, some of the most important criteria for it is usually, besides working, that it's understandable, maintainable and extendable.

Especially for code that is meant to be continued developed, if it isn't understandable, maintainable and extendable, it has failed to meet its requirements, and the people having made it have not done their job properly.

Jeff Ratcliff

Posts: 242
Nickname: jr1
Registered: Feb, 2006

Re: What is the Worst Code You've Ever Seen? Posted: Jan 8, 2007 9:01 AM
Reply to this message Reply
> That's a reasonable position. However, possibly unless the
> code is a one-off script, and in particular for code that
> is meant to be further developed, some of the most
> important criteria for it is usually, besides working,
> that it's understandable, maintainable and extendable.
>
> Especially for code that is meant to be continued
> developed, if it isn't understandable, maintainable and
> extendable, it has failed to meet its requirements, and
> the people having made it have not done their job properly.

Well, if those characteristics are, in fact, actual requirements than your point is consistent with mine. However, in general, there may be trade-offs between those characteristics and other goals of a project.

Extensibility really has an implicit prediction of the future built into it and its value approaches zero as the correctness of the prediction approaches zero.

I've been on a number of projects where significant effort went to trying to accommodate the future only to find later that all that was accomplished was an increase in development time and complexity.

In my view, there are no default requirements.

Merriodoc Brandybuck

Posts: 225
Nickname: brandybuck
Registered: Mar, 2003

Re: What is the Worst Code You've Ever Seen? Posted: Jan 8, 2007 9:04 AM
Reply to this message Reply
> IMHO the only universal and objective criteria for bad
> code is that it doesn't fulfill its requirements. Any
> other criteria that is applied without a specific context
> is subjective.


I used to think that. However, there is some code I've inherited over the years that made me rethink that.

If code unnecessarily opens and closes extra database connections in the same loop within 3 lines of each other, that's bad.

If code implements a sorting routine on an array of values returned from a database query such that the sorting takes too long to complete on less than 1,000 records (On the web version the page would time out. The desktop version took over a minute to sort ~1000 records) when you could have used 'ORDER BY' and returned your data already sorted in a fraction of a second for many, many thousands of records, that's bad.

Then there is the C++ that is really thinly veiled C that is generally considered bad. I've seen, and written, plenty of that.

As others have already noted, The Daily WTF has some "stellar" examples. http://thedailywtf.com/Articles/The_Data_Cleanup.aspx

As others here have admitted, I've written more than my share of 'bad' code. It always did what was required, but that doesn't make it not bad. Granted some of that is subjective, but there are enough things that most developers seem to agree upon as 'good' that I think there is something more to it.

Jeff Ratcliff

Posts: 242
Nickname: jr1
Registered: Feb, 2006

Re: What is the Worst Code You've Ever Seen? Posted: Jan 8, 2007 9:39 AM
Reply to this message Reply
>
> If code unnecessarily opens and closes extra database
> connections in the same loop within 3 lines of each other,
> that's bad.
>
> If code implements a sorting routine on an array of values
> returned from a database query such that the sorting takes
> too long to complete on less than 1,000 records (On the
> web version the page would time out. The desktop version
> took over a minute to sort ~1000 records) when you could
> have used 'ORDER BY' and returned your data already sorted
> in a fraction of a second for many, many thousands of
> records, that's bad.
>

If the scenarios that you site actually meet the requirements of the project (which I doubt since reasonable efficiency is usually a requirement) than I disagree. Non-optimal applications that fully meet their requirements don't really bother me in a business sense.

Faster is usually better (except perhaps in some real-time scenarios), but there's always a trade-off between optimization and the costs of performing it. Obviously you sited extreme examples, but the trade-off is always there.

Merriodoc Brandybuck

Posts: 225
Nickname: brandybuck
Registered: Mar, 2003

Re: What is the Worst Code You've Ever Seen? Posted: Jan 8, 2007 10:59 AM
Reply to this message Reply
> >
> > If code unnecessarily opens and closes extra database
> > connections in the same loop within 3 lines of each
> other,
> > that's bad.
> >
> > If code implements a sorting routine on an array of
> values
> > returned from a database query such that the sorting
> takes
> > too long to complete on less than 1,000 records (On the
> > web version the page would time out. The desktop
> version
> > took over a minute to sort ~1000 records) when you
> could
> > have used 'ORDER BY' and returned your data already
> sorted
> > in a fraction of a second for many, many thousands of
> > records, that's bad.
> >
>
> If the scenarios that you site actually meet the
> requirements of the project (which I doubt since
> reasonable efficiency is usually a requirement) than I
> disagree. Non-optimal applications that fully meet their
> requirements don't really bother me in a business sense.
>

> Faster is usually better (except perhaps in some real-time
> scenarios), but there's always a trade-off between
> optimization and the costs of performing it. Obviously you
> sited extreme examples, but the trade-off is always there.

I've only personally seen one spec that ever had any kind of efficiency explicitly stated as a requirement. So, according to you, since there are no default requirements, this shouldn't be a problem.

You say that the examples are extreme. Extreme or not, I had to fix them. I agree in principle but these problems seem so common. I've never seen any developer defend his work successfully by saying something like "It may run like a 3 legged dog, but it meets the specified requirements." I've seen them try, but they fail miserably. Reasonable efficiency is, in my experience, always a default requirement. In my career if I had to pick one type of problem I fixed the most, it would be off by one errors. A very close second would be "make it perform in a 'reasonable' manner."

There are also two other default requirements in my experience. Failing gracefully and acting in a manner users expect. These two aspects are rarely, if ever, explicitly stated in the projects I've worked on but you'll get hell to pay if you don't do either one of those.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: What is the Worst Code You've Ever Seen? Posted: Jan 8, 2007 11:31 AM
Reply to this message Reply
> If the scenarios that you site actually meet the
> requirements of the project (which I doubt since
> reasonable efficiency is usually a requirement) than I
> disagree. Non-optimal applications that fully meet their
> requirements don't really bother me in a business sense.
>
> Faster is usually better (except perhaps in some real-time
> scenarios), but there's always a trade-off between
> optimization and the costs of performing it. Obviously you
> sited extreme examples, but the trade-off is always there.

I'm against premature-optimization too (I think that what you are talking about here) but I've seen code that was more complicated and less efficient than another way of doing it. Things like making a O(n) operation into a O(n²) operation. Often code like this can meet requirements initially but then the size of the data set doubles a few times and it's no longer acceptable. I once found some code that was taking 30 seconds to a minute to open a view because it was looping over sorted data and restarting at the beginning after each found element. I deleted a 100 or so lines of code and made it run in less than a second. Did it meet requirements? I guess, because the users didn't know any better but it surely made them a lot happier when I 'fixed' it.

So I think we are not talking about suboptimal code here. We are talking about code that is used when a better solution would take a roughly equivalent amount or work. A good example is some code I was working with where there was a table of Strings to those same Strings with no spaces. I had to modify this application and I had no idea what this was about and there were few comments. In order to modify it, I had to figure out what this was or risk breaking it. After a number of hours, I finally realized that it was being used to pass the Strings as parameters to a web page. The developer didn't know how to encode the spaces. Again, I was able to delete code and a table and the resulting code didn't have to be maintained every time a new value needed to be passed. This was code that met requirements.

Erik Engbrecht

Posts: 210
Nickname: eengbrec
Registered: Apr, 2006

Re: What is the Worst Code You've Ever Seen? Posted: Jan 9, 2007 6:40 AM
Reply to this message Reply
> I've seen in a code review of a C++ class in production :
>
>
> /* in an instance method */
> if (this == null) {
> return 0;
> }
>

>
> In a way, it was not harmful but it made me think the
> developper missed something about object...

Actually, I think it is harmful. Some (all?) C++ compilers don't generate instructions that dereference this until a member variable is accessed. Consequently, inserting that code will make the method "work" when called on a null pointer.

It was probably inserted as a "fix" to a segfault that should have been fixed by not calling a function on a null pointer.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: What is the Worst Code You've Ever Seen? Posted: Jan 9, 2007 6:45 AM
Reply to this message Reply
> Actually, I think it is harmful. Some (all?) C++
> compilers don't generate instructions that dereference
> this until a member variable is accessed. Consequently,
> inserting that code will make the method "work" when
> called on a null pointer.

That seems kind of useful to me.

Frank Silbermann

Posts: 40
Nickname: fsilber
Registered: Mar, 2006

Re: What is the Worst Code You've Ever Seen? Posted: Jan 9, 2007 7:09 AM
Reply to this message Reply
>> If code implements a sorting routine on an array of values returned from a database query such that the
>> sorting takes too long to complete on less than 1,000 records ... when you could have used 'ORDER BY' and
>> returned your data already sorted in a fraction of a second for many, many thousands of records, that's bad.
>
> If the scenarios that you site actually meet the requirements of the project (which I doubt since
> reasonable efficiency is usually a requirement) than I disagree. Non-optimal applications that fully meet their
> requirements don't really bother me in a business sense.

Suppose the custom-crafted bubble-sort _did_ meet the performance requirements. Every future programmer who maintains that code will have to spend time and energy -- and therefore, the employer's money -- studying those lines of code to discover and verify that a sort is being done -- which the 'ORDER BY' clause would have made obvious.

By distracting the attention of new maintenance programmers, the developer's clumsy use of the API increases the time it will take to fix bugs or make enhancements -- which could be very costly to the business.

The employer will also have a harder time keeping good maintenance programmers, as they resent spending lots of time on things that increase neither their understanding of their craft nor their understanding of the employer's business.

Erik Engbrecht

Posts: 210
Nickname: eengbrec
Registered: Apr, 2006

Re: What is the Worst Code You've Ever Seen? Posted: Jan 10, 2007 6:06 AM
Reply to this message Reply
> Suppose the custom-crafted bubble-sort _did_ meet the
> performance requirements. Every future programmer who
> maintains that code will have to spend time and energy --
> and therefore, the employer's money -- studying those
> lines of code to discover and verify that a sort is being
> done -- which the 'ORDER BY' clause would have made
> obvious.
>
> By distracting the attention of new maintenance
> programmers, the developer's clumsy use of the API
> increases the time it will take to fix bugs or make
> enhancements -- which could be very costly to the
> business.
>
> The employer will also have a harder time keeping good
> maintenance programmers, as they resent spending lots of
> time on things that increase neither their understanding
> of their craft nor their understanding of the employer's
> business.

But suppose it's something more complex than a sort. For example, let's say there's some purely functional calculation that involves walking a mutable DAG. The easiest way to implement the calculation is to just calculate it using recursive calls to connected vertices.

A number of things can be done to optimize this. You can track which nodes have already been processed and avoid reprossing them just because more than one edge points towards them. You can cache calculated values so repeated calls don't require recalculation if nothing changed.

All these strategies make the code more complex. I would say they should not be used unless performance requirements are clearly not being (or going to be) met without them.

Merriodoc Brandybuck

Posts: 225
Nickname: brandybuck
Registered: Mar, 2003

Re: What is the Worst Code You've Ever Seen? Posted: Jan 10, 2007 6:40 AM
Reply to this message Reply
> > Suppose the custom-crafted bubble-sort _did_ meet the
> > performance requirements. Every future programmer who
> > maintains that code will have to spend time and energy
> --
> > and therefore, the employer's money -- studying those
> > lines of code to discover and verify that a sort is
> being
> > done -- which the 'ORDER BY' clause would have made
> > obvious.
> >
> > By distracting the attention of new maintenance
> > programmers, the developer's clumsy use of the API
> > increases the time it will take to fix bugs or make
> > enhancements -- which could be very costly to the
> > business.
> >
> > The employer will also have a harder time keeping good
> > maintenance programmers, as they resent spending lots
> of
> > time on things that increase neither their
> understanding
> > of their craft nor their understanding of the
> employer's
> > business.
>
> But suppose it's something more complex than a sort. For
> example, let's say there's some purely functional
> calculation that involves walking a mutable DAG. The
> easiest way to implement the calculation is to just
> calculate it using recursive calls to connected vertices.
>
> A number of things can be done to optimize this. You can
> track which nodes have already been processed and avoid
> reprossing them just because more than one edge points
> towards them. You can cache calculated values so repeated
> calls don't require recalculation if nothing changed.
>
> All these strategies make the code more complex. I would
> say they should not be used unless performance
> requirements are clearly not being (or going to be) met
> without them.

In my experience this is simply a matter of when. At some point performance will become an issue.

It's my opinion that the more complex the code, the more it behooves you to make it reasonably efficient the first time around. You are already talking about a somewhat complex problem and making it more complex is only a matter of degree. If you wait until performance becomes an issue you need to take the time and energy to figure out what you were doing in the first place before you can optimize the code. Better to make it as solid as possible while it is fresh. I wouldn't go out of my way to over optimize it, but if there are relatively common things you know will improve the efficiency in the average case, I would do them instead of waiting until it became a problem.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: What is the Worst Code You've Ever Seen? Posted: Jan 10, 2007 6:58 AM
Reply to this message Reply
> But suppose it's something more complex than a sort. For
> example, let's say there's some purely functional
> calculation that involves walking a mutable DAG. The
> easiest way to implement the calculation is to just
> calculate it using recursive calls to connected vertices.

I wouldn't call this bad code. Bad code IMO is when the approach makes things more complicated with no benefit. Naive code can be warranted.

Chad Gorshing

Posts: 12
Nickname: gorshing
Registered: Jun, 2004

Re: What is the Worst Code You've Ever Seen? Posted: Jan 10, 2007 6:18 PM
Reply to this message Reply
One of my favorites that I ran across in a C# project I inherited:


public void Dispose(DataSetHelper x)
{
x = null;
}


And no, the class did not implement from IDisposable.

Malte Finsterwalder

Posts: 20
Nickname: jufoda
Registered: Aug, 2003

Re: What is the Worst Code You've Ever Seen? Posted: Jan 11, 2007 1:11 AM
Reply to this message Reply
Some specific very bad things I saw in my career:

C-code written in Java. All the code was in the constructor and the main-method just created one object.

A Java class to parse XML-Data that inherited from GregorianCalendar, because the XML had some timestamp that needed to be converted.

This XML-parser read a data package format description. The configuration was stored in two seperate XML files. Some of the configuration was hardcoded into the parser as well. It "worked", but was very obscure and hard to understand. It took me about as long to adapt it than to throw it away and rewrite it. I rewrote it with a real solution to the problem that worked flawlessly and was easy to configure and extend.

So what it bad code to me?
It's not easy to say.

Bad code is hard to understand and to maintain. Bad code doesn't comunicate well what it does.
Bad code makes bad use of libraries and language features.

Flat View: This topic has 45 replies on 4 pages [ « | 1  2  3  4 | » ]
Topic: New IBM Tutorial: Geronimo Beans and the EJB Query Language Previous Topic   Next Topic Topic: Adobe Ships FlexBuilder for the Mac

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use