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 | » ]
Dirk Detering

Posts: 16
Nickname: det
Registered: Jul, 2005

Re: What is the Worst Code You've Ever Seen? Posted: Jan 11, 2007 1:15 AM
Reply to this message Reply
Advertisement
The code that I remember as "worst" was a somewhat small but seriously meant Java program written by -I think- a former C developer.
It consisted of only one class containing public static variables and methods plus the main method.
Should be clear: The whole thing worked without ever using the keyword 'new'.

(No one should try to argue that it perhaps was an ingeniously application of a better fitting functional paradigm, even in a Java context).

I think this could perhaps be an example of bad code which fulfils the requirements, as I get the feeling that "requirements" are mainly the things defined in likewise named papers and mainly deal with results, where topics like that -understanding paradigm of a used language- are unspoken expectations one developer has against professionally written code and has more to do with how to achieve the results.

More commonly: I think that there is more to differenciate "good code" from "bad code" than fulfilling requirements,
i.e.: common expectations of how things should be done and what is "quality" in code, even if they are not defined in the project's requirement papers.

And I really like the statement about the psychological relation between the code and its programmer in this thread.

Jonathan O'Connor

Posts: 1
Nickname: ninkibah
Registered: Sep, 2005

Re: What is the Worst Code You've Ever Seen? Posted: Jan 11, 2007 4:04 AM
Reply to this message Reply
I once got a job where I had to maintain and beautify an interpreter for a DSL. This was the companies core technology, and it had been written by the CTO, a Russian emigre. Although it was written in C++, most of the code consisted of functions hundreds of lines long, without any white space, and each line had about 4 statements. It had been obfuscated purely to protect the CTO's job, and ensure his visa would be renewed. I'm amazed that he was ever allowed to write the code that way, but it happened.

I left after 11 days. I have no idea what happened to the code.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: What is the Worst Code You've Ever Seen? Posted: Jan 11, 2007 6:50 AM
Reply to this message Reply
> It had
> been obfuscated purely to protect the CTO's job, and
> ensure his visa would be renewed.

It sounds like it worked.

Darryl Mataya

Posts: 9
Nickname: dmataya
Registered: Nov, 2004

Re: What is the Worst Code You've Ever Seen? Posted: Jan 11, 2007 6:56 AM
Reply to this message Reply
Great thread - the contents of this should be wrapped up, edited, and published in a little booklet that all novice and practitioner programmers keep handy.

I think the worst code I see generally falls into the category of "I know I'm supposed to be a programmer, but I'm not 100% sure the computer/compiler is going to do what I want." For example:

If (a > b)
...plan A
Else If (a <= b)
...plan B
Else
...just in case, plan X
End If

This sort of attitude, and it is an attitude - not just simple coding mistakes, leads to insidiously bad code to try and debug and maintain.

Another common example of this coding attitude is someone who creates numerous extraneous local variables as though the program will behave better if there are these little insurance policies scattered throughout. I recall one junior programmer who, no matter how many people tried to convince him otherwise, believed that these two snippets were not functionally identical:

f = Numericfunction(a,b,c)
g = f - 1
if g < 0 ...

vs.

if Numericfunction(a,b,c) - 1 < 0 ...

I think he somehow believed that if he didn't clearly spell out the execution order as he imagined it, the alternate code would mysteriously misbehave at inopportune times.

jens bendig

Posts: 9
Nickname: jensbendig
Registered: Jul, 2006

Re: What is the Worst Code You've Ever Seen? Posted: Jan 11, 2007 7:20 AM
Reply to this message Reply
Hello Darryl,

yes, the thread ist great. Unfortunately I don´t agree with you:

The first example: I think, it´s a good idea, always to formulate the else-branch, even if not necessary and there i.e. through an Error. Just in Case, some other coder changes the if-Branches or adds some other conditions, you cannot know that. The construction as a whole will keep stable. I don´t think it´s an attitute if you look at that in a bigger context and specify plan X to "throw an Error".

The second example: If I agree or not does strongly depend upon the context we are programming in: If g stands for a well understood concept, it´s a good idea to name g in a separate variable, just to let everybody know, that we are not only talking about f-1 but we identify this value as the well-known g-thing. In some cases, this maybe bad code, in some other cases, where g means something commonly understood, it´s very good code.

Yours,

Jens

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: What is the Worst Code You've Ever Seen? Posted: Jan 11, 2007 8:13 AM
Reply to this message Reply
> This sort of attitude, and it is an attitude - not just
> simple coding mistakes, leads to insidiously bad code to
> try and debug and maintain.

Well in that case, it should probably be an if-else and that's all but adding an else that throws an Exception can save someones ass when they are modifying the code.

For example, if you have a set of numeric constants or enums that you are doing an if-else-if chain on, and you make one of the values assumed on an else, that can lead to a bug if someone adds a constant and doesn't update the if-else. If you throw an exception in an else that says "hey you just introduced a bug", they should be able to catch it before it corrupts the database or whatever.

> I think he somehow believed that if he didn't clearly
> spell out the execution order as he imagined it, the
> alternate code would mysteriously misbehave at inopportune
> times.

Sometimes I agree, sometimes I don't. If that was in a language where true and false are numeric values, it might make sense to break it out so that people don't have to run to their syntax reference to see what it does. If it were Java, it's probably OK because you can't subtract a boolean from a number.

Jeff Ratcliff

Posts: 242
Nickname: jr1
Registered: Feb, 2006

Re: What is the Worst Code You've Ever Seen? Posted: Jan 11, 2007 9:22 AM
Reply to this message Reply
> Sometimes I agree, sometimes I don't. If that was in a
> language where true and false are numeric values, it might
> make sense to break it out so that people don't have to
> run to their syntax reference to see what it does. If it
> were Java, it's probably OK because you can't subtract a
> boolean from a number.

James: while I don't think you completely agree with my general point:

"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."

you've just provided an example of how context can clarify the issue of quality. Of course context has many dimensions and the choice of programming language is just one of them.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: What is the Worst Code You've Ever Seen? Posted: Jan 11, 2007 9:45 AM
Reply to this message Reply
> James: while I don't think you completely agree with my
> general point:
>
> "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 fully agree with it. There is no question in my mind that 'bad code' is a subjective thing. What I'm not sure we agree upon is whether it's still a useful way to classify code.

I don't think every judgement needs to be absolutely objective. Its good to have as much objective criteria as possible in making judgements but in the end, the vast majority of real-world decisions are at least somewhat subjective. I've seen a lot of really bad decision making come from the basis that objective (e.g. quantifiable) criteria only be used. Actually the philosophy that only objective criteria is meaningful is completely subjective.

> you've just provided an example of how context can clarify
> the issue of quality. Of course context has many
> dimensions and the choice of programming language is just
> one of them.

I never meant to argue that context wasn't important. It's probably the most important thing in judging code quality. Actually code that could be considered OK for it original purpose could be considered 'bad' in a future context. It doesn't mean the developer was bad. It doesn't even mean he or she was short-sighted. And again, I don't think sub-optimal is bad. Sub-optimal can be good. What I think is always bad (an opinion) is code that is overly complex while at the same time making only negative impact on other important criteria (such as speed, memory usage, correctness.) In other words doing more work to make something worse than the naive solution. I've never heard an argument showing why it would sometimes be good but if you have one, lay it on me.

Merriodoc Brandybuck

Posts: 225
Nickname: brandybuck
Registered: Mar, 2003

Re: What is the Worst Code You've Ever Seen? Posted: Jan 11, 2007 10:29 AM
Reply to this message Reply
> What I think is always bad (an opinion) is code
> that is overly complex while at the same time making only
> negative impact on other important criteria (such as
> speed, memory usage, correctness.) In other words doing
> more work to make something worse than the naive solution.
> I've never heard an argument showing why it would
> d sometimes be good but if you have one, lay it on me.

This might be a little bit of a stretch, but it's the best I can come up with :-)

http://www0.us.ioccc.org/main.html

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: What is the Worst Code You've Ever Seen? Posted: Jan 11, 2007 10:47 AM
Reply to this message Reply
> This might be a little bit of a stretch, but it's the best
> I can come up with :-)
>
> http://www0.us.ioccc.org/main.html

That reminded me of this:

http://mindprod.com/jgloss/unmain.html

Gordon Milne

Posts: 3
Nickname: gordy
Registered: Aug, 2006

Re: What is the Worst Code You've Ever Seen? Posted: Jan 11, 2007 12:15 PM
Reply to this message Reply
Hello Jens,

I agree with Darryl. I thought the point he was trying to make was that:

if (a > b)
plan A
else if (a <= b)
plan B
else
plan X
endif
should have been written as:

if (a > b)
plan A
else
plan B
endif
i.e. there is no reason at all for "plan X" to exist. It is pointless and is a symptom of coding style that tries to "catch all condition", even those that don't make any sense.

The only place I see this kind of code is with developers who never quite grasped something fundamental when they were learning to program. These developers often have severe problems understanding how pointers work (see http://www.joelonsoftware.com/articles/ThePerilsofJavaSchools.html for some ideas why understanding pointers might be considered a good idea).

As for Darryl's second example, the only thing I would have done is write it as:

if (Numericfunction(a,b,c) - 1) < 0 ...


I tend to use parenthesis a lot in comparisons since many, many years ago, I decided that learning operator precedence for was a game for mugs and added little to code clarity.

Jeff Ratcliff

Posts: 242
Nickname: jr1
Registered: Feb, 2006

Re: What is the Worst Code You've Ever Seen? Posted: Jan 11, 2007 1:31 PM
Reply to this message Reply
> I tend to use parenthesis a lot in comparisons since many,
> many years ago, I decided that learning operator
> precedence for was a game for mugs and added little to
> code clarity.

You just reminded me of a great source of "bad" code: the code used in language tests given as a part of an interview.

A common example is having the candidate determine what value a variable is going to be assigned based on a complicated expression without parenthesis. This sort of test seems to have little logical purpose: if the company makes reasonable use of parenthesis in its code, this skill is not required, if they don't, then demonstrating knowledge of a small subset of operator precedence rules are inadequate.

If they really don't use parenthesis in their code, the test should just ask you to write the precedence table for the language in question and forego the superfluous code snippet.

In general if the company actually uses any code you see on such a test in a production environment you probably don't want to work there.

Jeff Ratcliff

Posts: 242
Nickname: jr1
Registered: Feb, 2006

Re: What is the Worst Code You've Ever Seen? Posted: Jan 11, 2007 1:41 PM
Reply to this message Reply
> I never meant to argue that context wasn't important.

You didn't. I just didn't want to assume what your opinion was until you'd expressed it.

> good. What I think is always bad (an opinion) is code
> that is overly complex while at the same time making only
> negative impact on other important criteria (such as
> speed, memory usage, correctness.) In other words doing
> more work to make something worse than the naive solution.
> I've never heard an argument showing why it would
> d sometimes be good but if you have one, lay it on me.

I can't think of any good reason to add unecessary complexity to code either.

Jay H

Posts: 1
Nickname: jfh
Registered: Jan, 2007

Re: What is the Worst Code You've Ever Seen? Posted: Jan 11, 2007 3:03 PM
Reply to this message Reply
Meeting requirements is only one, though important, measure of good software. Perfomance, maintainability, extensibility, etc. are other measures of quality.

And if your requirements are bad, what does that say about the quality of the code? I have a collection of some really awful requirements, here are some of my favorites:

"2. If no formula exists, the system shall inform the user with an exceptional error message."

"The system will support a disaster at the facility."

"Added a note that Bob feels a portion of this requirement is 'incorrect'. I added note for Bob to ignore this requirement."

The ultimate measure of quality is customer satisfaction throughout the lifecycle of the application.

Carsten Saager

Posts: 17
Nickname: csar
Registered: Apr, 2006

Re: What is the Worst Code You've Ever Seen? Posted: Jan 12, 2007 12:38 PM
Reply to this message Reply
The worst code is too long to paste here ... and it can't be really appreciated if shortened: There are 4 parameters, all optional. For some (many) arbitrary combinations there had been a class taking some (one all, one none) of these parameters and implementing nothing but the ctor by setting the remaining values to 0 - of course they already had this value from the super-class ctor!

There is no extension of these classes, not even a comparision with instanceof, but a bunch of methods that work on these different object - all created via copy&paste.

Perhaps not the worst code I have seen, but the freshest impression after I discovered this as I just finished the 0,3kloc solution for any possible combination (I had to three special cases not yet covered)

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