Why Active Record Isn’t A Bad Design Pattern
Out Of Date Warning
Languages change. Perspectives are different. Ideas move on. This article was published on March 16, 2010 which is more than two years ago. It may be out of date. You should verify that technical information in this article is still current before relying upon it for your own purposes.
Were I writing this as an article for a newspaper, the subhead would be “Design Patterns Don’t Cause Application Slowness.” The point of this piece isn’t to defend Active Record per se; it’s to discuss the fact that design patterns aren’t to blame for your application’s problems, and more to the point, design patterns aren’t the problem.
This discussion stems from a discussion with a friend of mine who swore up and down that Active Record was a terrible design pattern that was inefficient, poorly designed, and ill-suited for use by developers. As a PHP developer, it’s easy to embrace this call, especially since it’s always fun to trash Ruby on Rails folks; however, I’m not willing to take the position that design patterns are the cause of performance problems.
Design patterns are examples of common problems and their solutions. Active Record, along with Decorator, Factory, Flyweight, etc. are all examples of common problems with code-based solutions. That’s it! Design patterns try, in a code-based example, to solve a common problem that developers need to solve.
Some patterns are better at solving certain problems than others; there are varying kinds of ways to interact with a database besides the Class-Table Instance-Row behavior of Active Record; implementing one design pattern over another is a decision of personal choice and need. But design patterns themselves do not cause performance problems.
What causes performance problems? Databases. Hardware bottlenecks. The specific implementation of the design pattern. There are all kinds of factors that relate to creating performance problems or scalability issues that have nothing to do with the design pattern itself. And while it’s easy to look at certain patterns and proclaim that they are diametrically opposed to good performant software, it’s much harder to realize that every pattern has its place, even in some of the best software in the world.
Design patterns aren’t about picking an efficient implementation and running with it. They’re about solving programming problems. Blaming design patterns for performance issues is ultimately blaming the wrong culprit. When we use patterns for their intent – to solve difficult, common problems – they are extremely helpful, regardless of the performance implications. Active Record, like every other pattern, isn’t a bad design pattern when it solves our problem – even if we have to rethink performance in the process.