Avoid Premature Generalization

You’ve no doubt heard me rant against premature optimization in the past, but Eric Gunnerson points out another “Premature” action to be avoided, “Premature Generalization”.

His discussion centers around a very specific question of whether to use private properties to access private fields, or just allow access to the field. Note this discussion pertains to fields that are not publicly accessible via property nor direct access.

The place you’ll often see premature generalization is when inexperienced developers start applying Design Patterns everywhere. If you need to instantiate a factory, implement an adapter class and use a bridge to the toilet just to take a dump, then you probably live with a developer with a premature generalization problem.

Like optimization, generalization is good when it is applied judiciously in the right places. With optimization, one should measure measure measure before applying optimizations. With generalization, I typically suggest that a developer must feel the pain first before generalizing. That simply means that the lack of generalization is starting to cause more work than it saves. In my experience, this often boils down to the rule of threes. If you have to implement something a third time, refactor it.

For example, suppose you have an import tool for some system and as far as you know, you’ll only have to support one import client. By all means write an importer specific to that client. Now your boss tells you to implement an importer for another client. Write that one specific to that client. Once again your boss tells you to implement an importer for yet another client. At this point a pattern has been established. Your boss is a liar and you’ll probably need to implement importers for many clients. Now is the time to refactor the code and generalize the concept of importers. Maybe create a plug-in model or an Import Provider.

[Listening to: Cass & Slide / Perception - Sasha - Sasha: Global Underground: Ibiza [2 of 2] (9:27)]

What others have said

Requesting Gravatar... Sharp as a Marble Sep 19, 2005 4:56 AM
# re: Avoid Premature Generalization
I've been known to have a problem with premature commenti....
Requesting Gravatar... Ryan Farley Sep 19, 2005 5:05 AM
# re: Avoid Premature Generalization
Yes, I've been known to suffer from premature generalization from time to time.

http://ryanfarley.com/blog/archive/2004/04/30/570.aspx

Not a problem I am proud of, but a strugle I deal with every day... ;-)
Requesting Gravatar... you've been HAACKED Sep 19, 2006 1:41 AM
# re: A Few Questions For Subtext Users
re: A Few Questions For Subtext Users
Requesting Gravatar... you've been HAACKED Jun 11, 2007 4:05 PM
# Introducing Subkismet-The Cure For Comment Spam
Introducing Subkismet-The Cure For Comment Spam
Requesting Gravatar... Mike Seth Aug 24, 2008 6:21 PM
# re: Avoid Premature Generalization
To my experience, the kind of generalization you speak of works best in certain junction points of your software. For example, a factory pattern works well when applied to major replaceable components, but it's completely redundant to spawn instances of every class using a factory. In general, design patterns can't replace foresight, and foresight is achieved from experience.
Requesting Gravatar... Sijin Joseph Aug 24, 2008 9:08 PM
# re: Avoid Premature Generalization
I had written about a similar experience a while back, check it out at www.indiangeek.net/.../do-not-start-with-an-int...
Requesting Gravatar... Felipe Aug 24, 2008 10:09 PM
# re: Avoid Premature Generalization
Great example!

Premature * is evil.
Requesting Gravatar... Eric Aug 25, 2008 12:13 AM
# re: Premature * is evil.
Yes, premature implies that it's done too early. It wouldn't be too early if it didn't matter that it was done early.
Requesting Gravatar... Joel Aug 26, 2008 4:09 AM
# re: Avoid Premature Generalization
The author rightly says a developer must feel the pain first before generalizing. But foreseeing the pain and not generalizing is just being plain lazy. copy-pasting of code, only make the developers life more difficult.
From my experience, avoiding generalization always and diving straight into the problem hoping you would do it only if required sometimes costs more (time) than if you had designed and generalized the problem to begin with.
Requesting Gravatar... matt Oct 23, 2008 7:16 PM
# re: Avoid Premature Generalization
I would disagree with three instances of something. Two is enough pain for me to want to generalize, and a valid place to generalize from.

Obviously, the generalization I would use for two would probably just be an interface, but that would be enough to begin with. Wouldn't want to be too premature!

What do you have to say?

(will show your gravatar)
Please add 3 and 4 and type the answer here: