Jeremy D. Miller -- The Shade Tree Developer

Sponsors

The Lounge

Wicked Cool Jobs

Syndication

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Long Methods and Classes Are Evil
I've spent a great deal of time in the last week reverse engineering legacy VB6 code, and I'd like to rant a little bit. What's making my life harder is how large and complex the methods are in each class module. The code is very procedural, with little usage of subroutines. The VB functions are several pages long, if/then nesting goes 6 or 7 deep, and case statements span hundreds of lines. Unsurprisingly, the code is hard to maintain and brittle in regression testing. Finding the location of a piece of functionality requires arcane knowledge of the application or some amount of luck with CTRL-F.

If the application you are building or modifying is important or useful, it is a sure bet the code will have to be modified later as requirements change and problems occur. There are some easy steps to take to make your code more understandable. Good naming is obviously important, a variable named "a1" isn't very descriptive, especially when it is an untyped variant. Can you say "Mystery Meat?"

My personal pet peeve is really long methods and classes that do way too much. I'm a terrible slob in most other ways, but I want my code readily organized with each type of code on its proper shelf, err, class. The human brain can only process so much information at a time. Small methods can be seen and understood all at one time. Bugs can hide out in very long methods because there is simply more brush to hide in. Large case statements are veritable camouflage for lurking bugs.

Rules of Thumb for Class and Method Size

  1. Any method should fit into your IDE window so the entire method can be read at one time. I'd take it farther if you can. One of the few code metrics I pay attention to is the average lines of code per method. I strive for an average of not more than 5 lines of code per method.
  2. Too many private members. You know what I mean, if you see a class with an excessive number of private fields (say >10), the class is probably too big. I'm tracing a class with about 75 private fields this morning. Not pleasant. In Fowler's Refactoring book this is described as the code smell "Primitive Obsession."
  3. The size of the code file. Offhand, I'd say any class over 10k in size is getting too large for its britches. A class that is 200k in the file system is certainly too long. TestFixture classes might be an exception.
  4. Exception handling code and instrumentation tend to push methods to be much larger. Invest some thought in how to segregate this type of code away from the main functionality. Aspect Oriented Programming may justify its existence by merely doing this.
  5. Reduce the number of public methods in a class. Just picking a number, I would say less than 10 in most cases. There is a good discussion on this subject at http://c2.com/cgi/wiki?FewShortMethodsPerClass.


I'm not just picking on VB developers, because I've seen (and written) bad examples in other languages, including a C# system that is featured in an early case study on MSDN as a shining example of the wonders of .NET. At a former employer, my teammates and I inherited a large new supply chain system hacked together by a very prominent consultancy. I distinctly remember a coworker printing out a Perl script that contained an if/then branch spanning 18 pages.

As an aside I'm 2-3 years removed from any kind of serious VB6 work. I remember a time when I was a staunch pro-VB guy because of its productivity for building code rapidly and hated being looked down on by Java guys. After becoming quite used to C# coding with ReSharper and other toys, VB6 feels extremely clunky. Combining well-factored C# code with the advanced code navigation features from VS.NET itself and ReSharper, leads to code that is readily traced and reverse-engineered. Oh yeah, I forgot to mention that variants suck and COM sucks more. I do still hate being looked down on by Java guys, though.


Posted Tue, Apr 26 2005 5:08 PM by Jeremy D. Miller
Filed under:

[Advertisement]

Comments

Jeremy D. Miller -- The Shade Tree Developer wrote Use a refactoring checklist to pay down Technical Debt as you work
on Fri, Aug 12 2005 11:17 AM
If you've never heard the term "Technical Debt," check out Martin Fowler's definition and Ward...
Jeremy D. Miller -- The Shade Tree Developer wrote Application Re-write Wackiness
on Fri, Aug 19 2005 11:57 AM
Have you ever had to do a re-write of an existing application?  If you have, you'll know exactly...
Jeremy D. Miller -- The Shade Tree Developer wrote Application Re-write Wackiness
on Fri, Aug 19 2005 9:36 PM
Have you ever had to do a re-write of an existing application?  If you have, you'll know exactly...
Jeremy D. Miller -- The Shade Tree Developer wrote Application Re-write Wackiness
on Wed, Oct 26 2005 4:40 PM
To any colleagues that stumble across this, please don't take any offense.  It was a bad, bad day...
Jeremy D. Miller -- The Shade Tree Developer wrote Best of the Shade Tree Developer (with actual links!)
on Mon, Aug 7 2006 4:51 PM
Between being extremely short handed at work, tech' reviewing a new book, a
possible book proposal...
Jeremy D. Miller -- The Shade Tree Developer wrote Best of the Shade Tree Developer (with actual links!)
on Fri, Sep 1 2006 2:33 PM

Between being extremely short handed at work, tech' reviewing a new book, a possible book proposal

Jump between braces in Visual Studio. « Vadim’s Weblog wrote Jump between braces in Visual Studio. « Vadim’s Weblog
on Wed, Jul 9 2008 8:26 PM

Pingback from  Jump between braces in Visual Studio. « Vadim’s Weblog

Andrei Butnaru's blog wrote Programming links 07.11.2008
on Fri, Jul 11 2008 2:31 PM

Programming links 07.11.2008

DotNetKicks.com wrote Long Methods and Classes Are Evil
on Sat, Aug 9 2008 8:48 PM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Jo Diggity wrote re: Long Methods and Classes Are Evil
on Sun, Aug 10 2008 5:16 AM

I don't buy your less than 10 public methods. A class should be an organised unit of abstracted code that serves a single purpose. Sometimes this includes having dozens of methods. Your logic would mean that classes would be limited in functionality.

Perhaps you don't understand OOP?

Jeremy D. Miller wrote re: Long Methods and Classes Are Evil
on Sun, Aug 10 2008 11:09 AM

@Jo Diggity,

If a class has dozens of methods, I seriously doubt that it's serving more than a single purpose.

Mike Borozdin wrote re: Long Methods and Classes Are Evil
on Mon, Aug 11 2008 9:37 PM

Good points that reminds me of Martin Fowler and his book on refactoring where he recommends the same thing.

Eh eh wrote re: Long Methods and Classes Are Evil
on Tue, Aug 12 2008 12:52 PM

Max 5 lines per method? Give me a break...

Jeremy D. Miller wrote re: Long Methods and Classes Are Evil
on Tue, Aug 12 2008 1:45 PM

@Eh eh,

You're exactly the kind of blog reader that pisses me off to the point of giving up blogging.  The EXACT wording up there was:

"an average of not more than 5 lines of code per method"

Average of no more than 5.  Not no more than 5.

Idiot.

o.s. wrote re: Long Methods and Classes Are Evil
on Tue, Aug 12 2008 3:16 PM

Great post Jeremy. Unfortunately the people who really need to hear this message don't read blogs so they can imporve and they don't even realize how bad a job they are actually doing.

none wrote re: Long Methods and Classes Are Evil
on Wed, Aug 13 2008 5:35 AM

Well, String in Java is over 2000 line long, and Vector is over 1000 lines long (atleast in java 5).

And they are justified.

I agree with the methods part, but then length of the java files depends on the functionality associated with the class.

sohbet wrote re: Long Methods and Classes Are Evil
on Thu, Feb 26 2009 12:10 PM

thank you very much

sohbet

http://www.ircask.com

Jeremy D. Miller -- The Shade Tree Developer wrote What makes a system hard to work with?
on Thu, Mar 12 2009 3:31 PM

I’m getting started (after I finish 2 others that are already late) on a new article roughly titled

Community Blogs wrote What makes a system hard to work with?
on Thu, Mar 12 2009 4:01 PM

I’m getting started (after I finish 2 others that are already late) on a new article roughly titled

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Devlicio.us