« Sad, sad times | Main | Great tshirt »

The most useless unit test ever

This is an actual unit test I ran into today.. I've made minor modifications to the code just to make it appear less like taken straight out of some application (although it is) and perhaps make it more fun to read.. but other than that.. the test is exactly as I found it...

So what's wrong with it? If you have to ask you'll never understand... or at least not before you program for a few more years, go off.. code something.

But it has comments! Yes.. yes it does. A lot of them and every single one utterly useless.

How did I run into this gem? It continuously fails on my daily unit test run, I finally decided to investigate why.. and well, the reason is obvious. It checks for a hardcoded value that was likely deleted long time ago.. as you know, test databases get wiped on regular basis and stuff.

It's obviously not finished! You say.. actually hasn't been touched in ages in an actively developed project.. so I have to assume it is meant to be this way.

Why am I not posting this to daily wtf? I'm not sure it's actually submission worthy.. It's just well.. so.. oh well, that, er, yah.

So what does it actually test? As far as I can tell.. JDBC works like a charm, good job Sun!

Comments

“Incomplete” is my impression. It looks like whoever was writing this got sidetracked about 1/5th of the way into it but checked the code in anyway and then promptly forgot about it (or was hit by a bus). And it looks like it was written in lots of very small steps, rearranging the code with each step.

My favourite part is how it uses a “meadow_id” variable to abstract away a magic value, but the comment that tells you it needs to be kept up-to-date is next to the only place in the code where the variable gets used. Might as well hardcode the value, eh? The fact that the following comment obviously has nothing to do with the code it belongs to comes a close second.

The useless and lying comments remind me of a long-running thread I’ve been in for a while now on the Daily WTF forums, where some people seem to be arguing that no amount of comments is too much (“‘lots helps lots’” as they call it in Germany) while I’m trying to bring some sanity to the table.
http://thedailywtf.com/forums/45483/ShowPost.aspx

I think this code proves that there is such a thing as too many comments.. or at least too many completely useless comments.

I would agree...there are so many useless/redundant comments, I can barely read the code!

More comment than code... Damon's right, it's unreadable... almost like one of those 3D puzzles where you have to cross your eyes and put them out of focus for it all to become clear! Nice...

In all fairness, if you're using a development tool that displays comments and code in different color or font, that code would be very readible.

Overall, however, it reminds me of those freshmen CS majors who aren't really sure how to write comments but are so worried about losing points they comment everything. I suppose they figure that if every line of code is commented, then the important ones will be.

This is why I've always been a fan of having tests insert data that they need in the database as part of the test setup and then remove it as part of the teardown.

Functional tests like this should never rely on the existance of test data that may not actually exist.

When I was a teaching assistant in grad school, I would usually grade students on the quality of their comments, not just on the quantity. The first few assignments I'd always get a lot of submissions where people would have comments like:

x++; // add 1 to x

but I'd mark them wrong for it and take off points with an explanation of why that's not a useful comment. They'd usually figure it out pretty quickly. (If they complained about the severity of losing points for that I'd give them some of the points back but also tell them that in the future I wouldn't be so lenient.)