Everyone uses tomcat, and nobody believes the tiny minority of us who hate it. Tomcat sucks, plain and simple. For such an important piece of infrastructure, you’d like to think that smart people who know a thing or two about good java code were involved. So, in an attempt to prove my point, I will discuss a single class in Tomcat, and we can all have a look at how brilliant the code is. I specifically picked a class that was written by ‘important’ people, just to ensure you idiots know not to trust these monkeys ever, ever again. This is just ONE class, there are many many worse ones.
The class we’ll look at is Tomcat’s DefaultServlet, written by Remy Maucherat (jboss monkey now, I believe) and Craig youknowwho.
Where does one start, really? Any intern at my company who writes code like this would earn a serious ‘talking to’. It’s in fact probably wiser to just take them out back and shoot them, doing both them and the world a favour. Alas, opensores has no such safeguards, and both of these ******* are alive and well, still defecating out huge reams of code in the community’s general direction.
So let’s get started. The first issue is the fact that everywhere, Throwable is caught. Yes, even Error type exceptions are caught. Things that god and Sun never intended for applications to even try to handle, tomcat will (silently) catch and ignore. After all, when you run out of memory, best thing to do is keep going right?
We also have methods that happily check for exception message strings (search for broken pipe), so I’d advice you to never use that string in any of your own exceptions. The comment around that bit of code is also particular amusing. The exception is caught, next comment says //ignore it, and the next line? You guessed it, throw the exception!
If you’ve ever wondered what the urge to chew off your own face feels like, then look no further than the parseRange and copyRanges methods. It’s like the authors fell off the antipattern tree and hit every branch on the way down, while somehow also managing to do things to exceptions that’d make your mother blush. We have returning concrete collection types when an interface will do, a million random exit points halfway through the method, using exceptions for logic flow rather than…well…exceptions. Hell, copyRanges even RETURNS an exception (which is ignored, sometimes). Come on Craig, you work at Sun, just go down the hall and for ****’s sake, ask someone to explain exceptions to you. You’re bound to find SOMEONE who understands the very basics.
At no point in the code is any stream closed in a finally clause, needless to say.
The pain goes on and on, and never really ends with this class. We have an impressive ignorance of how basic numeric handling in java works, as evidenced by // To avoid 0.0 for non-zero file, we bump to 0.1. We also have renderSize and displaySize, both of which do equally ****ed up tulip.
Ok, so maybe these kids don’t know the basics of elegant java code, that’s fine, many people don’t. At least they know idiomatic usage of java, right? They understand things like naming methods, idempotent methods, and so on. Haha, right. Calling validate on a range can have an interesting side effect of modifying the end of that range. Cool huh? You could avoid that ludicrous I’m-too-*******-stupid-to-know-how-to-keep-it-valid-so-I’ll-guard-against-it approach by just having a length() method, that is calculated based on start and end. Anything that reduces the amount of work you people need to exert can only be a good thing, given your intellectual capacity.
This code is so abysmally bad in fact that I know of at least one person (this is NOT a joke) who uses it in interviews, asks people to identify all the things wrong with it. These are potential employees who I’m sure would be paid a lot less than Craig or Remy, but had they the same coding ability, wouldn’t be allowed in. The whole thing would make a fine contribution to dailyWTF, I’m sure.
Of course, I’m sure the tomcat fanboys would quickly whip out their collective penis and wave it about angrily. After all, it’s still popular, right? Who cares if the code is ugly, it does its job! Who cares if it’s riddled with inefficiencies, is confusing to read, can’t be maintained or picked up easily, or a million of those criteria that real developers actually care about, even though nobody else does? You’re right, nobody cares. Tomcat is a great example of how good code is, as I’ve always insisted, irrelevant.