When should code changes be checked into source control

version control

I've heard conflicting philosophies on this issue – or specifically, two conflicting philosophies.

One is to check in the code as soon as the change is complete, so that your co-workers can see the change and to minimize any merging that has to be done when putting a build together.

The other is to not check in any code until you are certain that you are ready for a build, so that if a problem is found, you can address the issue as quickly as possible.

Whether or not one of these is correct, what is a good guideline for knowing when you should check in source controlled code?

This question suggests using branches to resolve this issue, but our version control (ClearCase) doesn't offer branching – so what option do we have for version control policy?

Best Answer

I come from the side that believe commits should be frequent. Exactly how frequent will depend on your personal development style. Some in the TDD world will commit every few minutes (each completed cycle), others perhaps a few times a day at what they feel to be a good point. Some benefits of frequent commits are:

  • Work is backed up, useful not just in case of disk failure or some other system problem, but human issues such as leaving your laptop at home (meaning you can jump on a spare dev system if one is available) and pick up where you left off, or illness (someone else can pick up your work)
  • Related to that, the ability to roll back if you realise you have gone down the wrong path at some point, without necessarily having to throw everything you have done away
  • Meaningful comments with the commit will provide a development story, helping you and other team members understand the change
  • Reduced likelihood of merge conflicts, as a result of using small incremental commits. Where conflicts occur, they are easier to resolve
  • In a CI environment, you will be notified early of any build failures or if you have broken any tests, and can fix them while the work is still fresh and before they cause issues for other team members

Instead of branching you can use feature toggles. These can be used to keep your feature hidden on separate code paths until it is ready to be revealed. However this does introduce some complexity and extra overhead, so it's generally not worth doing this for small tasks.

I'm unclear what you mean by "if a problem is found, you can address the issue as quickly as possible". Commits are quick and cheap, you fix the bug and make another one (commit, not bug). If on the other hand you mean "oh no I've found a major problem with my feature and want to pull the whole thing from the build", feature toggling can also help in this regard, as you just switch it off.