Saturday, August 12, 2006

Gross Code

Mike Bowler finds some gross code. Unfortunately, there's tons of examples in the JDK. My favorite has to be the Collections class in java.util. Why you ask? For example, the method sort(List) really should have been on the List interface. I also don't like the fact that it modifies the List and doesn't return a copy. I'm of the opinion that utility classes are usually a warning sign of lazy design. I understand that you do need them, but I try to hide their usage behind my objects. I never expose them.

2 comments:

Ekalavya said...

Shouldn't the Purse be modeled as an object in its own right with the usual methods for adding and taking out money?

Doesn't the token 'TOTAL_AMOUNT' denote total amount rather than the position at which the total amount is stored?

What is wrong is writing succint yet clear code? Do you always recommend computing an expression, store it in avariable, and refer to it at its single place of usage?

I don't think religiously following some coding guidelines leads one to elegant code. Not all single letter variables are bad. I have read your blogs for a long time and nver found any reason to disagree with anything. But, I am afraid you are spreading the same superficial concepts of what is good code as many text books and teachers.

Can you take a look at something you really admire and check how many of your favorite gudielines are broken but, in the big scheme of things, it doesn't matter in the least?

Elegance should be sought at all levels but design elegance is more critical as it is pervasive. Localized bad code is easy to handle by rewriting or ignoring. Bad design spreads itself a lot more.

Unknown said...

I agree with your points. It's just that if the low level code is sloppy, it usually is a good indicator to look at the design too. Care should be taken at all levels. We are crafts people.

I generally don't get stuck on every detail (I do let some things slide), but I do scrutinize sloppy low level code more. I usually don'y get upset until a lot of guidelines are broken. One or two violations don't bother me and I don't expect perfect code.

I would rather have elegant design as well, but lack of detail at the low-level usually means lack of detail at the design level as well.