Abstract the company away (part 1)
While my department at $work isn't strictly R&D, we still do a lot of development work. I'd dare say we average a rather healthy amount of code, taking into account we also try to be rather minimal. Code is simply a means to an end, it is not your goal.
We get to write a lot of code that interacts with our systems, and a lot of duplication is achieved by mistake. Sometimes it's stuff people don't realize is indeed duplication, or they think it's unpreventable. I've decided to tackle this. I've presented some of my findings at Weborama after giving my Moose talk, and they really enjoyed the ideas, and I've decided to write about them here as well, in more detail.
Since this is a lot of text and I hate reading a lot at a single time, I decided to parcel it out to smaller portions. This part will focus on refactoring schemas.
We started with code that needed to access a database, so we used the really-kickass DBIx::Class. Once we defined our schema, we were able to reduce our code and increase our readability by quite a bit! Unfortunately code that was irrelevant to the current project had to use the schemas as well. At first smaller portions of those schema was planned to be embedded. However, instead, we decided to separate the schemas.
I started by creating MyCompany::Schemas which included schemas for our major databases. Inside MyCompany::Schemas you had MyCompany::Schema::Kitties and MyCompany::Schema::KittieBeds. Then all code was converted to use MyCompany::Schemas.
At some point we noticed we have a problem with slow performance. What we noticed is that we're using two schemas which connect to the same database, and thus creating two different connections. How silly. DBIx::Class of course supports providing one host for multiple schemas. You just have to configure it correctly. Once we've done that, our performance was greatly increased.
Now that we have our schemas, we can push all the useful searches that are done in more than one application to the schema objects. Instead of each application doing the same search, just have a method in the schema that provides that search.
That's it for now! I hope you found it useful.
Following posts will discuss much bigger and more elaborate refactoring for database authentications, user access control and system-wide configurations for applications.