Organizing Shared Code
| January 22nd, 2010I spent a little bit of time last weekend looking into ways I can more effectively share code between projects. I’ve gone back and forth on ways to do this and I finally feel satisfied with what I’ve got. Really I have only one requirement for the method I use to share code among projects: I want to be as lazy as possible. For this reason, I had been just keeping shared code in one place and copying whatever files I want into projects when need. It doesn’t get any simpler. The problem of course is that this leaves me with no way to keep shared code in sync.
I’ve tried a couple of other methods of sharing code. Private frameworks are pretty heavy and don’t really offer than granularity that I want. I’d end up having a lot frameworks for a single class. Dynamic and static libraries have similar issues. And all of these have another potential problem: when I copy code into a project, I’ve essentially frozen that code so I know exactly what I’m working with. If I’m using a framework, I’m a little more likely to update that framework and accidentally break some project that’s using it.
Well, I finally decided my laziness was leading to more work, so I spent a bit of time learning about git submodules. I won’t go into any detail about using git or submodules. There’s excellent info about submodules here: Git Submodule Tutorial. Basically when I decide a class or group of classes is worth sharing, I’ll separate it into it’s own project (and git repository of course) with it’s own tests to keep it as isolated as possible. When I want to use the class or classes, I simply add the repository as a submodule.
I’m happy with it so far, and I feel a lot better about not just simply copying files. It’s still early and I have yet to go through tagging and branching with submodules, so there could be other issues that come up.
You mentioned you include your class tests inside the submodule repo. This will automatically lead to those test files ending up in a subdirectory of your superproject’s repo. Do you have a way of dealing with that clutter, or don’t you mind?
The clutter doesn’t bother me. It also leaves me with the option of changing code in the submodule (along with the tests) if I want to and either pushing it back or not. I’m not sure of a solution if the clutter did bother me.