How to avoid code duplication across unrelated projects [duplicate]

I’m a contractor at a large Telco where I’m usually working on several different projects at once.

The VCSs I use (mainly git and mercurial) tend to make me keep the code bases for unrelated projects in separate repositories, and I realise many benefits of this approach.

However, there are many little bits of utility code and helper functions I’ve written which are useful across two or more projects, and I’m well aware of the benefits of keeping my code DRY – for example sometimes I find a better way to implement some piece of code and I don’t want to have to update it in every place where I’ve duplicated the same idea.

What is the best way to refactor out this common functionality (it is not always so simple as copy-and-paste, but sometimes it is..) without creating unnecessary dependencies between unrelated projects, and without adding needless complexity?

1

Well, “little bits of utility code” are probably best not shared across unrelated projects. Just look at how many implementations of “Hello, World!” are out there. 🙂

Seriously, DRY is not an absolute. There is a cost for searching for an existing solution and a relatively high maintenance cost for sharing library code across otherwise independent projects. One project wants to add functionality but another project doesn’t, so what do you do? You fork it or add an option, and now you’re not really sharing the code anymore. Or worse one project adds a feature but it breaks some other project. The more people use a library the harder it is to change.

You see a very big and practical example of this right now in the Ruby on Rails community. Projects rely on dozens of gems (hefty bits of utility code) and then they are afraid to update them because something will break because “improvements” in one library break the expectations of another. It’s managed to a large degree with sensible version numbers that segregate changes by the likelihood of breaking existing code, but still, there’s a lot of overhead to versioning and managing those version numbers, and it’s just not worth it for 20 lines of code.

As a rule of thumb, if you’re the first person to write it, it’s not that useful. If Large Telco doesn’t already have that functionality in the codebase such then no one else felt it was important. More likely, it’s in there somewhere and you can’t be bothered to find it because it’s easier to just write it yourself. And you’re probably right, but so are your colleagues who want to just write it themselves rather than go look for it.

In practice, the best advice I can give is to keep your own personal library of code snippets and carry them around with you. Copy and paste from other projects if you like. As long as the snippets are small and don’t contain real secrets, like cryptographic keys or the formula for Coca-Cola, no one is going to bother you about taking code from one company to another let alone from one project to another.

And then when a colleague asks “hey, do you know a good way to do X” you can just copy and paste the code into an email or IM and be the hero of the hour.

Factor the common stuff out into one or more libraries. Put the library in its own repo, and in the individual projects, add a bit of logic to your build script to make sure you have the correct version of the library. You may or may not fancy subrepos for this; matter of taste, really.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật