Next Week’s Assignment

Today is Saturday, at least here in the USA it is. (I think it’s Thursday south of the equator, right?) At any rate, here is your homework for Monday when you return to “work”:

Find the longest human-coded class in a Java application at your company. Also, find the longest human-coded method in a Java application. Post the results here.

This is Thanksgiving week here in the USA, so it’s not like any of you are actually working next week. This should be fun!

Great Blog

For “longest”, let’s just count the number of “\n” characters.

Bonus points to anyone who knows of a simple program that can produce these metrics by quickly scanning an entire directory tree. Counting lines of code in a class is easy with a bit of shell scripting mojo, I’m not so sure about counting lines of code in individual methods.

I suppose you might try to briefly explain the reason these classes and methods are so abnormally huge, without exposing anything proprietary. We wouldn’t want to embarrass anyone, after all.


Sam.Halliday Says:

Dang… the human coded restriction stops me claiming a gigantic 3MB abstract class I just generated for the netlib libraries (99% of that is in javadoc comments though) and soon to be a part of f2j.

Lucian Says:

I worked at a company that made J2ME 2D games.
Because of the limitations of the Java VMs on mobile phones there were between 2 and 5 classes per project (if you were porting a game from a “normal” device onto a “stupid” device you had to merge some classes). Also because of these limitations you had to code stuff in a little number of functions . Usually the state-machine was a humongous piece of code (~ 10.000 lines) and stuff got put into functions only if it had to be reused more than once or twice.
I quit that place and never looked back :)

Paul Says:

If you use Eclipse, it’s easy to work the per-method stat out with the plugin at http://metrics.sourceforge.net/

Note that this actually counts lines of code, so it excludes comments and blank lines. Our stats, therefore, are actually WORSE than the below but I couldn’t be bothered working out any other way to calculate lines of SOURCE in a method.

In terms of classes (which is obviously a lot easier; wc -l is your friend), we have a class which staggers over line at 6514 lines of source. This is a servlet controller which manages to neatly include plenty of ‘M’, a fair bit of ‘V’, and more ‘C’ than you could pile on with a shovel. It’s pretty ghastly; thankfully we’re in the middle of rewriting that module now so that should be going down drastically soon.

For methods (measured in LOC, not LOS, per the above), we have a big ugly switching method in another controller, which clocks in at (and I am completely unconvinced this is a coincidence) 666 lines of code (cue spooky music). Unsurprisingly, that also has the highest cyclomatic complexity in the codebase (162!).

Eric Burke Says:

Looks like PMD might be able to help us out also, although I haven’t tried it. I think we may have one or two classes where I work that beat that 6514 lines, but I’ll have to check on Monday to be sure. It’s gonna be close.

Alex Miler Says:

I’ve used JavaNCSS for this in the past. Works well, gives you an XML report on your whole code base. The associated Ant stuff is crap though and doesn’t work, so don’t waste your time. I actually wrote my own Ant task to wrap it and was going to release but never got around to it (and have changed jobs since).

Fun idea, but I wonder about the metrics of counting “\n”. It could get messy.

I think we need a clarification (a) are comments counted? (b) what about nested classes?

It is certainly easier to just count # of lines but the implications about complexity become dangerous.

Eric Burke Says:

@Mike, there’s no danger here. I’m just looking for the most horrific code out there. Someone’s going to report back with some absurdly huge method clocking in at 23000 lines of code. At those numbers, whether you count comments or not, it’s just comically bad.

And where is your Gravatar, by the way???

Alex Miler Says:

In case my reference to JavaNCSS was not precise enough above, check out my blog post for a sketch.

I got 707 as the longest human-coded method in our source.

Alex Miler Says:

Ok, ok, I added a Gravatar. Testing here to see if it works.

Dan Says:

At a previous employer we had a class that was ~13,000 lines long. It was the monolithic “persistence layer”. I’d just like to say: it was like that when I got there.

Eric Burke Says:

I just found a class with 9,093 lines. I have a few more projects to test. No really big methods, though.

I already have found some classes that have more than 15k lines! And I remember a case where the developer was reach the limit size for a Java method (64kb). Oh my God. :-O