I encounter this on a fairly regular basis - a project uses a third-party library and there is either a bug in the library that we can't seem to avoid hitting, or there's a feature missing or not 100% ideal for our use case.
Especially when dealing with an open source library, at this point someone will inevitably suggest that we have the source, so we should just fix/hack/modify the library and get on with our lives. I'm massively opposed to that approach, with essentially one exception I'll mention towards the end.
So, why am I so opposed to changing a third-party library, even if I have the code and the rights to do so?
It's very simple - it adds to the maintenance headache. If I suddenly find myself having to change outside code, I have to be able to maintain it going forward. That means I:
- Suddenly have to have a place to keep it in source control so I can maintain my patches going forward
- Updating the library becomes an exercise in merging the local changes with the remote changes and should the author of the library make substantial structural changes, it's not even clear if you can easily integrate the new version into your patched version. You might well end up in a situation where you locked yourself out of taking future updates because the effort involved in porting your changes forward is so big that you can't reasonably accommodate it. You find yourself in a dark room, owning another piece of code that by rights, you really shouldn't and it's going to unnecessarily increase your maintenance effort because all of a sudden, you're not only responsible for bugs in your own code, but also bugs in the third-party library code that isn't so third-party library anymore.
There is one exception I'd make to the rule of not touching third-party code and that is, if you talked to the maintainer/owner of the library, agreed that what you're trying to do is actually beneficial to everybody involved and most importantly, that the owner of the library is willing to take your change and integrate it into their next release. Most open source projects will be more than happy to accept contributions this way as long as they fit in with their vision of how the code should work. Same goes for commercial libraries, too. Just talk to them beforehand to make sure it's not a misunderstanding of the code at your end.
Under those particular circumstances, yes, I'd accept that it's OK to change third-party code. Any other reason, don't do it.
Butbutbutbut, I absolutely have to change an important third-party library because their API doesn't fit into our model anymore?
No you don't, sorry. Either replace the library or write a shim that takes care of your needs, because I don't need to have a discussion with you in a few years time as to why we aren't able to upgrade a library that we decided we needed to hack and now are tied to an old code base that's potentially buggy and full of security holes.